Am 15.03.2014 00:08 schrieb "Fred van Stappen" <fi...@hotmail.com>:
>
> > Note: if your MyString is valid through the complete lifetime it could
> > be used you can also use "Result := PChar(MyString);"
>
> Hum, to be sure that i understand...
>
> type
>   TStringClass = class(TObject)
>     Name: AnsiString;  //// does not change
>     SomeText: AnsiString; ///// may change..
>   end;
>
> var
> AStringClass : TStringClass;
>
> ....
>
> function GetName: PChar;  cdecl ;
> begin
> result := PChar(AStringClass.Name);
> end;
>
> function GetSomeText: PChar;  cdecl ;
> var
>    MyString: AnsiString ;
> begin
>  MyString := AStringClass.SomeText ;
>
>    Result := GetMem(Length(MyString) + 1);
>    Move(@MyString[1], Result, Length(MyString));
>    Result[Length(MyString)] := 0;
> end;
>
> Is it OK ?

You don't need the MyString variable, you can simply use
AStringClass.SomeText. The variable was just for illustrations.

And you should definitely document the results of which of your libraries
must be freed and which must not.

Additionally I would suggest to store the pointer values of the allocated
PChar results (e.g. TList or specialize TFPGList<PChar>) so that you can
use this inside your exported FreeString function to check whether this is
indeed a pointer that you need to free.

Regards,
Sven
_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply via email to