Darren Ferguson wrote:
> function GetDescriptor(ID: PWideChar; var Descriptor: TDescriptor):
> Integer;
>   var
>     AClass: TMeDIIntfFormClass;
> begin
>   if WideSameText(WideString(ID), TfrmOhmedaBiox.IntfID) then
>   begin
>     AClass := TfrmOhmedaBiox;
>   end
>   else
>   begin
>     AClass := nil;
>   end;
>
>   if Assigned(AClass) then
>   begin
>     Descriptor.ID := PWideChar(AClass.IntfID);

Since IntfID is a function returning WideString, that line is actually
compiled as though it were like this:

var
  TempIntfID: WideString;

TempIntfID := AClass.IntfID;
Descriptor.ID := PWideChar(TempIntfID);

When this GetDescriptor function returns, the local variables are
released. That includes the TempIntfID variables. Therefore, Descriptor.ID
is left pointing at memory that has already been released.

The most straightforward way to solve that is to use WideStrings instead
of PWideChars. Then the compiler manages the strings' lifetimes for you.

Otherwise, you need to allocate memory for the Descriptor.ID field in
advance. You can use the JclWideStrings unit to do this:

Descriptor.ID := StrNewW(PWideChar(AClass.IntfID));

That allocates a new PWideChar buffer and fills it with the contents of
the IntfID string. Use StrDisposeW when you're finished with
Descriptor.ID.

-- 
Rob


_______________________________________________
Delphi mailing list -> [email protected]
http://www.elists.org/mailman/listinfo/delphi

Reply via email to