Sign in Join

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

Mailing lists / fpc-pascal / 2011-12

Memory leak

6 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

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

Unknown author

While dabbling with pointers, I came across a memleak for which I don't understand why it happens. Hopefully someone can help me with it.

mytest^.name := Format('%s.%.3d', ['name', 1]);

This assigns
'name.001' to the pointer variable but causes a memleak. If I assign the same value as string constant no memleak occurs, so it must be something related to format.

Regards, Darius

Reply by email

Jonas Maebe
to Unknown author
Show quoted text 6 lines
mytest^.name := Format('%s.%.3d', ['name', 1]); This assigns 'name.001' to the pointer variable but causes a memleak. If I assign the same value as string constant no memleak occurs, so it must be something related to format.

It's related to
a) the type of "name" (I assume it's an ansistring or unicodestring) b) how you free the memory to which mytest points

If you use freemem or reallocmem, then you will get a memory leak because freemem/reallocmem do not finalize the memory before freeing it. You have to manually call finalize() on the records inside that memory block prior to freeing them via freemem/reallocmem. dispose on the other hand will finalize the freed memory automatically.

Jonas

Reply by email

Unknown author
to Jonas Maebe
Jonas Maebe wrote
On 18 Dec 2011, at 14:04,
[1] wrote
> mytest^.name :=

Format('%s.%.3d', ['name', 1]); This assigns 'name.001' to the pointer variable but causes a memleak. If I assign the same value as string constant no memleak occurs, so it must be something related to format.

It's related to a) the type of "name" (I assume it's an ansistring

or unicodestring)

b) how you free the memory to which mytest points If you use freemem or reallocmem, then you will get a memory leak

because freemem/reallocmem do not finalize the memory before freeing it. You have to manually call finalize() on the records inside that memory block prior to freeing them via freemem/reallocmem. dispose on the other hand will finalize the freed memory automatically.

Jonas_______________________________________________

fpc-pascal

maillist - [2]

http://lists.freepascal.org/mailman/listinfo/fpc-pascal [3]

It was an
ansistring indeed, and finalize did the trick! Thanks for your help. How does New and Dispose work btw? Do they use RTTI to call finalize appropriatly? I tried searching the New and Dispose implementations in the RTL sources, but I just could not find them. What unit are they in?

Regards, Darius

Links:
------
[1]

[2]

[3]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

Jonas Maebe
to Unknown author
It was an ansistring indeed, and finalize did the trick! Thanks for your help. How does New and Dispose work btw? Do they use RTTI to call finalize appropriatly?

Yes.

I tried searching the New and Dispose implementations in the RTL sources, but I just could not find them. What unit are they in?

They are compiler internals. The compiler translates them into an appropriate combination of getmem/initialize or finalize/freemem.

Jonas

Reply by email

Unknown author
to Jonas Maebe
Jonas Maebe wrote
On 18 Dec 2011, at 15:45,
[1] wrote
> It was an ansistring indeed,

and finalize did the trick! Thanks for your help. How does New and Dispose work btw? Do they use RTTI to call finalize appropriatly?

Yes.

> I tried searching the New and Dispose implementations in the

RTL sources, but I just could not find them. What unit are they in?

They are compiler internals. The compiler translates them into an appropriate combination of getmem/initialize or
finalize/freemem.

Thanks Jonas, that makes things more clear for me.

Regards, Darius

Links:
------
[1]

Reply by email

Unknown author
to Jonas Maebe: It's related to a) the type of "name" (I assume it's an ansistring or unicodestring) b)…
Jonas Maebe wrote
If you use freemem or

reallocmem, then you will get a memory leak because freemem/reallocmem do not finalize the memory before freeing it. You have to manually call finalize() on the records inside that memory block prior to freeing them via freemem/reallocmem. dispose on the other hand will finalize the freed memory automatically.

I searched the documentation. Are finalize
and initialize documented at all?

Regards, Darius

Reply by email