[fpc-pascal] Library.StringFunction() : PChar => NO
Sven Barth
pascaldragon at googlemail.com
Sat Mar 15 07:33:28 CET 2014
Am 15.03.2014 00:08 schrieb "Fred van Stappen" <fiens at 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20140315/1b3c31d6/attachment.html>
More information about the fpc-pascal
mailing list