Board index » delphi » Destroying dynamically created objects

Destroying dynamically created objects

Hi there,

I'm writing a chat client/server app. When a new client connects to the
server, a client object is created dynamically that contains the connection
ID and some other information about the client. I then store the
clientobject-pointer in a TStringList using addobject. If the client
disconnects I want the object to be destroyed. How do I do this?
StringList.Delete just removes the pointer, the object still exists.

Wybe

 

Re:Destroying dynamically created objects


Wybe -

Quote
>I'm writing a chat client/server app. When a new client connects to the
>server, a client object is created dynamically that contains the connection
>ID and some other information about the client. I then store the
>clientobject-pointer in a TStringList using addobject. If the client
>disconnects I want the object to be destroyed. How do I do this?
>StringList.Delete just removes the pointer, the object still exists.

Since you are _storing_ the object with myStringList.AddObject, you can't expect
MyStringList.Delete to destroy it.  You need to destroy it with
TObject(MyStringList.Objects[i]).Free().  Then you can call
MyStringList.Delete(i) to remove the (now useless) reference from the list.

--
Clayton Neff
Software Project Leader
The Personal Marketing Company, Inc.
11843 West 83rd Terrace
Lenexa, KS  66214
(913)492-0322
clay...@tpmco.com

Other Threads