Jump to content

Destructor of Object in TObjectList<k,v> never gets called despite doOwnsValues

Recommended Posts

Can someone help me? I use a TObjectDictionary<integer,TmesData> with doOwnsValues, but if I remove an object via .Remove(key) my destructor does not get called. My class declaration:

TmesData = class
    procedure socketDataAvailable(Sender: TObject; ErrCode: Word);
    payload: string;
    sendCount: integer;
    sendSocket: TWSocket;
    log: ts;
    sendTime: tdatetime;
    constructor create(alog: ts; const ip,port,apayload: string);
    destructor Destroy; //close and free socket
    function doConnect():boolean;
    function doSend():integer;

On my form I do

//create sendList
// add objects

later I do processing with

for var seq in sendlist.Keys do
  //do processing
  //remove current object after processing

The destructor of my TmesData objects never gets called. I must miss something obvious.

Share this post

Link to post
2 minutes ago, Vandrovnik said:

destructor Destroy; override;

Ah, yes. Obviously. I generally don't write my own objects (or they are only "records on steroids, so I don't need a custom destructor)

Share this post

Link to post
Posted (edited)

Not trying to be a smartass, but doesn't the compiler warn you explicitly?




[dcc32 Warning] Project1.dpr(7): W1010 Method 'Destroy' hides virtual method of base type 'TObject'


Edited by Der schöne Günther
  • Like 2
  • Thanks 1

Share this post

Link to post
12 hours ago, Der schöne Günther said:

Not trying to be a smartass, but doesn't the compiler warn you explicitly?


Some of us would notice this immediately because we make a concerted effort to minimize Hints and Warnings that the compiler produces. 


But for those who aren't so inclined, it's just another bit of noise to be ignored.

  • Like 1

Share this post

Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now