1
votes

I have a TObjectList<TUSBDevice>, where TUSBDevice is a class I've made. I tried calling Delete with the index passed as a parameter but that it simply does what TList.Delete() does: removes the pointer from the list but doesn't free the object itself.

The breakpoint I placed on TUSBDevice.Destroy() doesn't break when Delete() is called. I also had a watch on the TObjectList and I can see the item gets removed from the list but the contents at the memory address of the object don't get freed.

Destructor of TUSBDevice:

destructor TUSBDevice.Destroy();
begin
  removeDatabaseEntry();
  filteredFolders.Free();
  fileQueue.Free();
end;
1
Please provide minimal reproducible example Your question as-is does not have enough information to reproduce your issue. - Dalija Prasnikar
You likely forgot to mark the TUSBDevice destructor declaration with override: destructor Destroy; override;. Or your object list doesn't own its members. - Andreas Rejbrand
That was the problem Andreas, thanks. I always forget I need to do this. - Radu M.

1 Answers

2
votes

It's impossible to answer your question since it doesn't contain a minimal reproducible example; the issues doesn't lie in the code you posted, but elsewhere.

Still, the most common cause of an "overridden" destructor not running is that it is in fact not overridden. So I can almost bet that your Destroy declaration is missing the override:

TUSBDevice = class
  // ...
public
  // ...
  destructor Destroy; override;
  // ...
end;