TStringList/pchar question

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

TStringList/pchar question

Ryan Joseph
I just learned that pchar is not ref counted like I used to think so I’m curious why this code doesn’t fail. Shouldn’t this crash because I free TStringList and then access GetText?

strings := TStringList.Create;
strings.LoadFromFile(vertexShaderPath);
vertexShaderSource := strings.GetText;
strings.Free;
glShaderSource(vertexShaderID, 1, @vertexShaderSource, nil);

Regards,
        Ryan Joseph

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: TStringList/pchar question

Michael Van Canneyt


On Sun, 2 Jun 2019, Ryan Joseph wrote:

> I just learned that pchar is not ref counted like I used to think so I’m curious why this code doesn’t fail. Shouldn’t this crash because I free TStringList and then access GetText?

You're first doing gettext and then freeing. So this is OK.

>
> strings := TStringList.Create;
> strings.LoadFromFile(vertexShaderPath);
> vertexShaderSource := strings.GetText;
> strings.Free;
> glShaderSource(vertexShaderID, 1, @vertexShaderSource, nil);

You'll get a memory leak, unless you're freeing vertexShaderSource further
on.

See also
https://www.freepascal.org/docs-html/rtl/classes/tstrings.gettext.html

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: TStringList/pchar question

Ryan Joseph


> On Jun 2, 2019, at 2:48 PM, Michael Van Canneyt <[hidden email]> wrote:
>
>> I just learned that pchar is not ref counted like I used to think so I’m curious why this code doesn’t fail. Shouldn’t this crash because I free TStringList and then access GetText?
>
> You're first doing gettext and then freeing. So this is OK.

I thought pchar was ref counted so by calling GetText I was retaining ownership but GetText is allocating a new string (https://www.freepascal.org/docs-html/rtl/classes/tstrings.gettext.html). Sorry I didn’t read the documentation because I made a bad assumption about ref counting. Thanks.

Regards,
        Ryan Joseph

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal