title: How do I stop this Variant memory leak?
body: <p>I'm using an old script engine that's no longer supported by its creators, and having some trouble with memory leaks.  It uses a function written in ASM to call from scripts into Delphi functions, and returns the result as an integer then passes that integer as an untyped parameter to another procedure that translates it into the correct type.</p>
  
  <p>This works fine for most things, but when the return type of the Delphi function was Variant, it leaks memory because the variant is never getting disposed of.  Does anyone know how I can take an untyped parameter containing a variant and ensure that it will be disposed of properly?  This will probably involve some inline assembly.</p>
  
  <pre><code>procedure ConvertVariant(var input; var output: variant);
  begin
    output := variant(input);
    asm
      //what do I put here? Input is still held in EAX at this point.
    end;
  end;
  </code></pre>
  
  <p>EDIT: Responding to Rob Kennedy's question in comments:</p>
  
  <p>AnsiString conversion works like this:</p>
  
  <pre><code>procedure VarFromString2(var s : AnsiString; var v : Variant);
  begin
    v := s;
    s := '';
  end;
  
  procedure StringToVar(var p; var v : Variant);
  begin
    asm
      call VarFromString2
    end;
  end;
  </code></pre>
  
  <p>That works fine and doesn't produce memory leaks.  When I try to do the same thing with a variant as the input parameter, and assign the original <code>Null</code> on the second procedure, the memory leaks still happen.</p>
  
  <p>The variants mostly contain strings--the script in question is used to generate XML--and they got there by assigning a Delphi string to a variant in the Delphi function that this script is calling.  (Changing the return type of the function wouldn't work in this case.)</p>