1
votes
FUNCTION SystemspartsClT.KeyFound(Key : AluCostDict.SystemspartskeyT) : BOOLEAN;
VAR v :  Variant;
BEGIN
   v      := VarArrayCreate([0,1], VarInteger);
   v[0]   := Key.System;
   v[1]   := Key.PartType;
   Sucess := t.Locate('System;PartType', v, []);
   v      := NULL;
   Result := Sucess;
END;

I am using Delphi for Win32.

Does this function create a memory leak or not ?

Should I free the variant v as vararray and how?

Should I free or initialize the local variant v?

2

2 Answers

7
votes

It's not a leak, but it's way too much code for a fairly simple task. Try:

FUNCTION SystemspartsClT.KeyFound(
    Key : AluCostDict.SystemspartskeyT) : BOOLEAN;
BEGIN
   Result := t.Locate('System;PartType', 
                 VarArrayOf([Key.System, Key.PartType]), []);
END;
6
votes

No, no and no. Variants and variant arrays are managed by the compiler. They get initialized when you create them and freed when they go out of scope. The only way they could create a memory leak is if you assigned an object to the variant's value then forgot to free it.