Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2008-07

freeing string equal to setting to empty string?

11 messages · 6 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

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

Reply by email

Jonas Maebe
to Marc Santhoff
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.

Show quoted text 12 lines
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

Reply by email

Marc Santhoff
to Jonas Maebe
Jonas Maebe wrote 26 lines
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

Reply by email

Luca Olivetti
to Marc Santhoff
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

Reply by email

Mattias Gaertner
to Luca Olivetti
Show quoted text 13 lines
El Wed, 23 Jul 2008 17:15:13 +0200 Marc Santhoff 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

Reply by email

Joao Morais
to Luca Olivetti: El Wed, 23 Jul 2008 17:15:13 +0200 I may be wrong but I think you don't need to do…
Luca Olivetti wrote 10 lines
El Wed, 23 Jul 2008 17:15:13 +0200 Marc Santhoff 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

Reply by email

Mattias Gaertner
to Luca Olivetti
Show quoted text 10 lines
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

Reply by email

Marc Santhoff
to Luca Olivetti: El Wed, 23 Jul 2008 17:15:13 +0200 I may be wrong but I think you don't need to do…
Luca Olivetti wrote 10 lines
El Wed, 23 Jul 2008 17:15:13 +0200 Marc Santhoff 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

Reply by email

Marc Santhoff
to Joao Morais: Yup. Ansi strings, refcounted interfaces and dyn arrays are automagically freed when…
Joao Morais wrote 15 lines
Luca Olivetti wrote: > El Wed, 23 Jul 2008 17:15:13 +0200 > Marc Santhoff 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

Reply by email

David Emerson
to Marc Santhoff
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!)

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

~David.

Show quoted text 15 lines
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

Reply by email