title: Delphi TJSONArray Memory Leaks: Cannot FreeandNil JSON containers/objects
body: <p>The image below lists the memory leaks introduced by the following code:</p>
  
  <p>METHOD:</p>
  
  <pre><code>    procedure TInformationSource.SetConfig;
  var
    vJSONConfigList : TStringList;
    vCurrentFileName : string;
    vDebugString : string;
  
  begin
  
      if iCJSONByteStream = nil then
        iCJSONByteStream := TBytesStream.Create;
  
      if vJSONConfigList = nil then
        vJSONConfigList := TStringList.Create;
  
      for vCurrentFileName in uCInformationSourceConfigFileNames do
      begin
  
        iCJSONBytes := TFile.ReadAllBytes(vCurrentFileName);
        iCJSONAsStr := TFile.ReadAllText(vCurrentFileName);
  
        fMain.jdInformationSource.JsonText := iCJSONAsStr;
        fMain.jtvInformationSource.LoadJson(false);
  
        if iCConfigsList = nil then
          iCConfigsList := TStringList.Create;
        if iCJSONData = nil then
          iCJSONData := TJSONArray.Create; // TJSONObject.Create();
  
  
        iCJSONConfigData := TJSONObject.ParseJSONValue(iCJSONBytes , 0) as TJSONObject;
  
        vDebugString := iCJSONConfigData.ToString;
        iCEndpoint             := GetActiveEndpoint;
        iCHeaderParams         := GetActiveHeaderParams;
        iCActiveAuthParams     := GetActiveAuthParams;
        iCActiveProtocolParams := GetActiveProtocolParams;
        iCActiveSourceName     := GetActiveSourceName;
  end;end;
  </code></pre>
  
  <p>DESTRUCTOR OF OBJECT:</p>
  
  <pre><code>    destructor TInformationSource.Destroy;
  begin
  
    FreeandNil(iCHeaderParams);         // Okay
    FreeandNil(iCActiveProtocolParams); // Okay
    FreeandNil(iCJSONByteStream);       // Okay
    FreeandNil(iCConfigsList);          // Okay
    FreeandNil(uCInformationSourceConfigFileNames);  //Okay
  
    //FreeandNil(iCJSONConfigData);      // nil pointer access violation - Why?
    //FreeandNil(iCActiveAuthParams);    // nil pointer access violation - Why?
    //FreeandNil(iCJSONData);            // nil pointer access violation because TJSONArray Virtual base class?
    //FreeandNil(iCAuthPairs);           // nil pointer access violation because TJSONArray Virtual base class?
  
    inherited;
  end;
  </code></pre>
  
  <p>DECLARATIONS:</p>
  
  <pre><code>//...
         private
     iCJSONData             : TJSONArray;
      iCAuthPairs            : TJSONArray;
      iCHeaderParams         : TJSONObject;
      iCActiveAuthParams     : TJSONArray;
      iCActiveProtocolParams : TJSONObject;
      iCActiveSourceName     : String;
      iCJSONAsStr : string;
      iCJSONBytes : TBytes;
      iCJSONByteStream : TBytesStream;
      iCConfigsList : TStringList;
      iCJSONConfigData : TJSONObject;
  //...
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/ELEWU.jpg" alt="Memory leaks caused by the above code"></p>
  
  <p>Now the last 5 FreeandNil statements in the Destructor each deliver an access violation. For the TJSONArray instances my understanding at this time is that either you cannot FreeandNil TJSONArray objects because you cannot construct them with .Create in the first place (the compiler complains that you have instantiated an object from a class with virtual methods) - or else you can only FreeandNil them if they are instantiated using TJSONValue.GetValue. However I am not sure if this is true and if it is I am not sure why ythe difference. See the next paragraph. </p>
  
  <p>TJONValue is a Virtual base class of sealed and therefore unextendable classes TJSONArray and TJSONObject. I am creating instances of the latter objects with calls to TJSONObject.ParseJSONValue and TJSONValue.GetValue where X is the type of value (TJSONArray, TJSONObject) to be returned.</p>
  
  <p>Now it looks like I am getting access violations for FreeandNil on TJSONObject instances created by calls to TObject(TJSONObject.ParseJSONValue(...,TByteArray)) (which is returning valid values), while I am not getting access violations on TJSONObjects created with calls to TJSONValue.GetValue(keystring);</p>
  
  <p>I do not understand why this is.</p>
  
  <p>Are the TMoveArray leaks associated with the leaked TJSONArray objects as it seems?</p>
  
  <p>And additional question is - how does one free the memory associated with TBytes instance which are instances of the generic TArray where the type constraint is type Bytes? Some of the memory leaks in the image are associated with those also. Or is this done utomatically and therefore does not contribute to the memory leak list below.</p>