freeing string equal to setting to empty string?

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

freeing string equal to setting to empty string?

Marc Santhoff
Hi,

I understand right that (as I have asked before) an empty (ansi)string
is equal to NIL.

If this is correct, is setting a string to '' the same as setting the
string to NIL?

So i can e.g. use exchagably:

{$H+}
var
  s: string;
begin
  s := 'something';
 
  { this ... }
  s := '';
  { ... works equally to that: }
  s := NIL;

And more: is the memory consumed by the string freed using one of the
two methods?

TIA,
Marc


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

Re: freeing string equal to setting to empty string?

Jonas Maebe-2

On 23 Jul 2008, at 06:32, Marc Santhoff wrote:

> I understand right that (as I have asked before) an empty (ansi)string
> is equal to NIL.

True.

> If this is correct, is setting a string to '' the same as setting the
> string to NIL?

No.

> So i can e.g. use exchagably:
>
> {$H+}
> var
>  s: string;
> begin
>  s := 'something';
>
>  { this ... }
>  s := '';
>  { ... works equally to that: }
>  s := NIL;

No, because "s:=nil" doesn't compile. And pointer(s):=nil would create  
a memory leak.

> And more: is the memory consumed by the string freed using one of the
> two methods?

It is freed if you use s:='' (but keep in mind the earlier discussion  
about reference counted types: the memory may not be freed  
immediately, because there may still be hidden references lingering  
around for a while).


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

Re: freeing string equal to setting to empty string?

Marc Santhoff
Am Mittwoch, den 23.07.2008, 08:28 +0100 schrieb Jonas Maebe:

> On 23 Jul 2008, at 06:32, Marc Santhoff wrote:
>
> > I understand right that (as I have asked before) an empty (ansi)string
> > is equal to NIL.
>
> True.
>
> > If this is correct, is setting a string to '' the same as setting the
> > string to NIL?
>
> No.
>
> > So i can e.g. use exchagably:
> >
> > {$H+}
> > var
> >  s: string;
> > begin
> >  s := 'something';
> >
> >  { this ... }
> >  s := '';
> >  { ... works equally to that: }
> >  s := NIL;
>
> No, because "s:=nil" doesn't compile.

Mumble ...

>  And pointer(s):=nil would create a memory leak.

That's the important part, I'm dealing with cleanly freeing strings when
destroying objects.

> > And more: is the memory consumed by the string freed using one of the
> > two methods?
>
> It is freed if you use s:=''

This is the way to do it, OK.

> (but keep in mind the earlier discussion  
> about reference counted types: the memory may not be freed  
> immediately, because there may still be hidden references lingering  
> around for a while).

I will.

Many thanks,
Marc


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

Re: freeing string equal to setting to empty string?

Luca Olivetti-2
El Wed, 23 Jul 2008 17:15:13 +0200
Marc Santhoff <[hidden email]> escribió:

> That's the important part, I'm dealing with cleanly freeing strings
> when destroying objects.

I may be wrong but I think you don't need to do anything, when you free
a class the memory used by string fields is automagically freed
(either that or heaptrc is lying[*] when it says my programs aren't
leaking memory)

[*]though I know of at least a nasty leak with gtk2 under lazarus that
heaptrc didn't trace.

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

Re: freeing string equal to setting to empty string?

Mattias Gaertner
On Wed, 23 Jul 2008 19:21:00 +0200
Luca Olivetti <[hidden email]> wrote:

> El Wed, 23 Jul 2008 17:15:13 +0200
> Marc Santhoff <[hidden email]> escribió:
>
> > That's the important part, I'm dealing with cleanly freeing strings
> > when destroying objects.
>
> I may be wrong but I think you don't need to do anything, when you
> free a class the memory used by string fields is automagically freed
> (either that or heaptrc is lying[*] when it says my programs aren't
> leaking memory)
>
> [*]though I know of at least a nasty leak with gtk2 under lazarus that
> heaptrc didn't trace.

Which one?

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

Re: freeing string equal to setting to empty string?

Joao Morais
In reply to this post by Luca Olivetti-2
Luca Olivetti wrote:

> El Wed, 23 Jul 2008 17:15:13 +0200
> Marc Santhoff <[hidden email]> escribió:
>
>> That's the important part, I'm dealing with cleanly freeing strings
>> when destroying objects.
>
> I may be wrong but I think you don't need to do anything, when you free
> a class the memory used by string fields is automagically freed
> (either that or heaptrc is lying[*] when it says my programs aren't
> leaking memory)

Yup. Ansi strings, refcounted interfaces and dyn arrays are
automagically freed when declared as class members and you destroy the
instance.

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

Re: freeing string equal to setting to empty string?

Luca Olivetti-2
In reply to this post by Mattias Gaertner
En/na Mattias Gaertner ha escrit:

>> [*]though I know of at least a nasty leak with gtk2 under lazarus that
>> heaptrc didn't trace.
>
> Which one?

http://bugs.freepascal.org/view.php?id=9259
(I just noticed you fixed it, but heaptrc didn't report anything when I
first discovered it).

Bye
--
Luca

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

Re: freeing string equal to setting to empty string?

Mattias Gaertner
On Wed, 23 Jul 2008 22:54:08 +0200
Luca Olivetti <[hidden email]> wrote:

> En/na Mattias Gaertner ha escrit:
>
> >> [*]though I know of at least a nasty leak with gtk2 under lazarus
> >> that heaptrc didn't trace.
> >
> > Which one?
>
> http://bugs.freepascal.org/view.php?id=9259
> (I just noticed you fixed it, but heaptrc didn't report anything when
> I first discovered it).

Heaptrc only checks the FPC heap. But in this case it was gtk memory. So
you need something like valgrind.

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

Re: freeing string equal to setting to empty string?

Marc Santhoff
In reply to this post by Luca Olivetti-2
Am Mittwoch, den 23.07.2008, 19:21 +0200 schrieb Luca Olivetti:

> El Wed, 23 Jul 2008 17:15:13 +0200
> Marc Santhoff <[hidden email]> escribió:
>
> > That's the important part, I'm dealing with cleanly freeing strings
> > when destroying objects.
>
> I may be wrong but I think you don't need to do anything, when you free
> a class the memory used by string fields is automagically freed
> (either that or heaptrc is lying[*] when it says my programs aren't
> leaking memory)

So as a rule of thumb strings can be used like any other non-object and
non-heap variable, as long as they are declared in the context of a
class.

If other objects or vars are holding pointers, ref counting prevents
freeing strings still needed.

Marc


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

Re: freeing string equal to setting to empty string?

Marc Santhoff
In reply to this post by Joao Morais
Am Mittwoch, den 23.07.2008, 17:10 -0300 schrieb Joao Morais:

> Luca Olivetti wrote:
> > El Wed, 23 Jul 2008 17:15:13 +0200
> > Marc Santhoff <[hidden email]> escribió:
> >
> >> That's the important part, I'm dealing with cleanly freeing strings
> >> when destroying objects.
> >
> > I may be wrong but I think you don't need to do anything, when you free
> > a class the memory used by string fields is automagically freed
> > (either that or heaptrc is lying[*] when it says my programs aren't
> > leaking memory)
>
> Yup. Ansi strings, refcounted interfaces and dyn arrays are
> automagically freed when declared as class members and you destroy the
> instance.

That's interesting and nice. Can I assume the same is valid for dyn
arrays in a procedure or function context?

Normal vars do "behave" that way ...

program x;

procedure a;
var
  s: array of string;
begin
  SetLength(s, count);
  s[0] := 'a string';

  ... do something ...

end; { no freeing of the array strings needed? }


Marc

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

Re: freeing string equal to setting to empty string?

David Emerson
On Wednesday 23 July 2008 7:46 pm, Marc Santhoff wrote:
> That's interesting and nice. Can I assume the same is valid for dyn
> arrays in a procedure or function context?

The short answer is yes.

There was recently (about a month ago) a discussion about some of these
very issues, from 21 to 29 June. I'd encourage you to read it.

(Unfortunately I erroneously made the subject line "memory management
with open arrays and classes"; and I replied to another thread instead
of starting a new thread, so the messages don't appear in a nice little
self-contained thread. doh!)

http://www.mail-archive.com/fpc-pascal@.../msg12918.html

...and three messages following; make sure you read the one by Jonas, as
the first reply contains some misinformation.

~David.


>
> Normal vars do "behave" that way ...
>
> program x;
>
> procedure a;
> var
>   s: array of string;
> begin
>   SetLength(s, count);
>   s[0] := 'a string';
>
>   ... do something ...
>
> end; { no freeing of the array strings needed? }
Indeed not. Even if s was an array of ansistrings, they would still be
cleaned up automatically when the var goes out of scope.
>
>
> Marc

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