mounir_moussa
mounir_moussa

Reputation: 11

free the variant VarArray

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?

Upvotes: 1

Views: 2786

Answers (2)

Mason Wheeler
Mason Wheeler

Reputation: 84550

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.

Upvotes: 6

Craig Stuntz
Craig Stuntz

Reputation: 126547

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;

Upvotes: 7

Related Questions