title: Delphi 6 OleServer.pas Invoke memory leak
body: <p>There's a bug in delphi 6 which you can find some reference online for when you import a tlb the order of the parameters in an event invocation is reversed.  It is reversed once in the imported header and once in TServerEventDIspatch.Invoke.</p>
  
  <p>you can find more information about it here:
  <a href="http://cc.embarcadero.com/Item/16496" rel="nofollow noreferrer">http://cc.embarcadero.com/Item/16496</a></p>
  
  <p>somewhat related to this issue there appears to be a memory leak in TServerEventDispatch.Invoke with a parameter of a Variant of type Var_Array (maybe others, but this is the more obvious one i could see).  The invoke code copies the args into a VarArray to be passed to the event handler and then copies the VarArray back to the args after the call, relevant code pasted below:</p>
  
  <pre><code>  // Set our array to appropriate length
    SetLength(VarArray, ParamCount);
  
    // Copy over data
    for I := Low(VarArray) to High(VarArray) do
      VarArray[I] := OleVariant(TDispParams(Params).rgvarg^[I]);
  
    // Invoke Server proxy class
    if FServer &lt;&gt; nil then FServer.InvokeEvent(DispID, VarArray);
  
    // Copy data back
    for I := Low(VarArray) to High(VarArray) do
      OleVariant(TDispParams(Params).rgvarg^[I]) := VarArray[I];
  
    // Clean array
    SetLength(VarArray, 0);
  </code></pre>
  
  <p>There are some obvious work-arounds in my case:  if i skip the copying back in case of a VarArray parameter it fixes the leak.  to not change the functionality i thought i should copy the data in the array instead of the variant back to the params but that can get complicated since it can hold other variants and seems to me that would need to be done recursively.</p>
  
  <p>Since a change in OleServer will have a ripple effect i want to make sure my change here is strictly correct.  </p>
  
  <p>can anyone shed some light on exactly why memory is being leaked here?  I can't seem to look up the callstack any lower than TServerEventDIspatch.Invoke (why is that?)</p>
  
  <p>I imagine that in the process of copying the Variant holding the VarArray back to the param list it added a reference to the array thus not allowing it to be release as normal but that's just a rough guess and i can't track down the code to back it up.  </p>
  
  <p>Maybe someone with a better understanding of all this could shed some light?</p>