Skip to content

TObjectList<> leaves memory leak #15

@rsgrimes

Description

@rsgrimes

I have a class (TDemoFamily) where I use a TObjectList list to contain TDemoPersonobjects. I want to serialize and deserialize instances of TDemoFamily using DelphiJSON. It works in general, but there are two memory leaks involved, one of which I can handle.

Consider this code:

fam := DelphiJSON<TDemoFamily>.Deserialize(CTest); 
fam.members.OwnsObjects := True;
fam.Free;  // Leaks a hidden instance of TDemoFamily

When the first statement executes, it returns a properly constructed TDemoFamily, with two caveats:

  1. The members TObjectList list is created (line 1) with the "OwnsObjects" property set to False, which is unfortunate, as one needs to manually delete the list when deleting the TDemoFamily object. Fortunately, setting it True here (line 2) seems to resolve that, but it is something that needs to be remembered. (Note: the TDemoFamily constructor sets that property True.)
  2. When you later delete the family (line 3), the leak of a TDemoFamily instance occurs.

I've attached a simple example, a console app that when run, shows the leakage report. Included in the archive is a Hack fix for the other issue I created several days ago.

DelphiJSONLeaker.zip

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions