RTC Forums
November 24, 2024, 03:01:35 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   Home   Help Login Register  
Pages: [1]
  Print  
Author Topic: How to properly free objects in TRtcHttpServer sessions  (Read 5011 times)
GeirBerg
RTC License+
****
Posts: 13


« on: February 12, 2014, 12:22:26 PM »

We are currently working on implementing sessions in one of our new products.

Everything was going well until we started using Session.Obj[] in the TRtcHttpServer sessions. When a user logs out of the system we are able to correctly handle this in the TRtcHttpServer.OnSessionClose event. We free the objects and set the Session.Obj[] to nil.

However, this event does not trigger for open sessions when stopping the server / shuting down the application. This results in memory leaks.

How can we properly cleanup / close all the open sessions (and associated objects) on shutdown of the server?
Do we have to iterate through all the sesions somehow, or should we handle some other event maybe?

Thank you in advance!
Logged
D.Tkalcec (RTC)
Administrator
*****
Posts: 1881


« Reply #1 on: February 12, 2014, 04:25:20 PM »

There are no events called when the Server is shutting down and there is no manual way for closing all Sessions and forcing the OnCloseSession event to trigger. All Session object which are still open when the Server is terminating, will be destroyed in the finalization section of the TRtcDataServer component. Because this happens after all the Forms and DataModules have already been destroyed, it is crucial for the Session components to know exactly which objects should be freed and if they should be freed, how that has to be done. To accomplish this, the TRtcInfo class and all classes extending it (like TRtcSession) have a rule for handling objects assigned through the "Obj" or "Child" properties.

1. You can assign a pointer to an object which is maintained somewhere else and should be destroyed somewhere else. Any object which is NOT a descendant of the TRtcObject class fallsinto that category. When you assign an object to "Obj" or "Child" which is NOT a descendant of the TRtcObject class, the Session object will simply clear the pointer, without destroying the object. This will happen when the Session object is destroyed. Such objects are assumed to be owned by something else and should NOT be destroyed by the Session.

2. If you want the object which you assign to "Obj" or "Child" properties (on the Session object) to be destroyed with the Session, implement a custom class based on the TRtcObject class and make sure to implement the "Kill" method on your custom class to release the object and any memory it holds. When a Session is being destroyed (this happens during shutdown or after a Session closes), the Kill method will be called on all objects still assigned to the "Obj" and "Child" properties inside the Session.


Best Regards,
Danijel Tkalcec
Logged
GeirBerg
RTC License+
****
Posts: 13


« Reply #2 on: February 13, 2014, 12:58:57 PM »

Thank you very much!

I made the objects inherit from TRtcObject and implemented the Kill procedure, and now it's working fine Smiley

Just one small follow up question:

I had to call Free() on the object itself inside its own Kill procedure to prevent the memory leak. This seems quite dangerous to me, but I suppose its ok since all references to the object will be cleared immediately afterwards (ie. RTC internally sets its .Obj[] reference to nil)? Or am I doing it wrong?
Logged
D.Tkalcec (RTC)
Administrator
*****
Posts: 1881


« Reply #3 on: February 13, 2014, 03:19:54 PM »

Calling Free on the last line of a method implementation should be OK.

Best Regards,
Danijel Tkalcec
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.024 seconds with 17 queries.