Board index » cppbuilder » Owner of dynamic created instances in threads

Owner of dynamic created instances in threads


2003-11-05 10:23:52 AM
cppbuilder52
Dear all,
May be a simple question. I create threads in my application to handle
events from serial ports independently. In each thread if I "new" an
instance of classes, whose constructor requires an "Owner" (which is an
instance of TComponent) as the parameter. What appropriate action is at
this step? Since the thread itself is not inherited from TComponent, it
cannot be the Owner. If I invoked the constructor using the main form as
the owner, should I delete the instance in the thread's destructor? Or
should I leave the owner to take care of cleaning up the objects it owns?
Thanks and regards,
Patrick.
 
 

Re:Owner of dynamic created instances in threads

I would new the object in the threads constructor, with a NULL owner, then
delete it in the thread's destructor. Alternately, create objects in the
thread's Execute method (still with NULL owner), work with the objects in a
try/finally block, and delete them in the finally block.
Mark
"Patrick Wong" < XXXX@XXXXX.COM >wrote in message
Quote
Dear all,

May be a simple question. I create threads in my application to handle
events from serial ports independently. In each thread if I "new" an
instance of classes, whose constructor requires an "Owner" (which is an
instance of TComponent) as the parameter. What appropriate action is at
this step? Since the thread itself is not inherited from TComponent, it
cannot be the Owner. If I invoked the constructor using the main form as
the owner, should I delete the instance in the thread's destructor? Or
should I leave the owner to take care of cleaning up the objects it owns?

Thanks and regards,
Patrick.


 

Re:Owner of dynamic created instances in threads

This works fine, thanks Mark.
"Mark Guerrieri" < XXXX@XXXXX.COM >wrote in message
Quote
I would new the object in the threads constructor, with a NULL owner, then
delete it in the thread's destructor. Alternately, create objects in the
thread's Execute method (still with NULL owner), work with the objects in
a
try/finally block, and delete them in the finally block.
 

{smallsort}