ghkweon / dwscript

Automatically exported from code.google.com/p/dwscript
0 stars 0 forks source link

Memory leak in TdwsJSONValue.Detach #399

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
When calling Detach on a TdwsJSONValue owned by a TdwsJSONObject, (and possibly 
other cases,) the detached value will leak instead of being freed.

Here's the chain of events:

procedure TdwsJSONValue.Detach;
var
   oldOwner : TdwsJSONValue;
begin
   oldOwner:=FOwner;
   if oldOwner<>nil then begin
      FOwner:=nil;
      oldOwner.DetachChild(Self);
   end;
end;

TdwsJSONObject.DetachChild calls:

procedure TdwsJSONObject.DetachIndex(i : Integer);
var
   n : Integer;
   child : TdwsJSONValue;
begin
   child:=FItems[i].Value;
   if child.FOwner=Self then begin
      child.FOwner:=nil;
      child.DecRefCount;
   end;
   //skipping irrelevant code
end;

By the time TdwsJSONObject.DetachIndex is called, FOwner on the child object is 
already nil, so DecRefCount is never called on it.

Original issue reported on code.google.com by masonwhe...@gmail.com on 16 May 2013 at 4:37

GoogleCodeExporter commented 8 years ago
Detach is meant to detach the object without freeing it, so it can be attached 
somewhere else, or be kept around as a detached node/branch.
If you want to delete a value, you should be able to just call free.

Original comment by zar...@gmail.com on 16 May 2013 at 8:10

GoogleCodeExporter commented 8 years ago

Original comment by masonwhe...@gmail.com on 16 May 2013 at 12:51