Solved

memory leak in Delphi2.01 with basic functions

Posted on 1997-06-26
2
371 Views
Last Modified: 2008-03-10
Hi,

I read some stuff in the delphis newsgroups about memory leak. Some deals with
database access, some other with VCL.

I meet today memory leaks with basic functions like "IntToStr" used in a console application on NT4. And also with Win32 API such as "GlobalGetAtomName", ...
Does anyone else also experimented such behaviour? If yes, what can we do to work around? Buy Delphi3 (if it corrects these problems)?

Thanks in advance

laurent


ps:
Below is a little program to test this, in which I use the GetHeapStatus to get info about memory:

var
  heapAfter   : THeapStatus;

begin
  writeln('before inttostr(23)');
  GetHeapStatus(heapAfter);
  MemoryDisplay(heapAfter);  // this just displays the THeapStatus fields in a formatted way

  inttostr(23);
  GetHeapStatus(heapAfter);
  writeln('after inttostr(23), before intostr(235465)');
  MemoryDisplay(heapAfter);

  inttostr(235465);
  GetHeapStatus(heapAfter);
  writeln('after intostr(235465), before intostr(243445343)');
  MemoryDisplay(heapAfter);

  inttostr(243445343);
  GetHeapStatus(heapAfter);
  writeln('after intostr(243445343)');
  MemoryDisplay(heapAfter);
end;

The result is as follow:

before inttostr(23)
TotalAddrSpace: 1048576  TotalUncommitted: 1032192  TotalCommitted: 16384  TotalAllocated: 768  TotalFree: 15428  FreeSmall: 84  FreeBig: 15344  Unused: 0  Overhead: 188  HeapErrorCode: 0

after inttostr(23), before intostr(235465)
TotalAddrSpace: 1048576  TotalUncommitted: 1032192  TotalCommitted: 16384  TotalAllocated: 780  TotalFree: 15412  FreeSmall: 84  FreeBig: 15328  Unused: 0  Overhead: 192  HeapErrorCode: 0

after intostr(235465), before intostr(243445343)
TotalAddrSpace: 1048576  TotalUncommitted: 1032192  TotalCommitted: 16384  TotalAllocated: 784  TotalFree: 15408  FreeSmall: 100  FreeBig: 15308  Unused: 0  Overhead: 192  HeapErrorCode: 0

after intostr(243445343)
TotalAddrSpace: 1048576  TotalUncommitted: 1032192  TotalCommitted: 16384  TotalAllocated: 788  TotalFree: 15404  FreeSmall: 120  FreeBig: 15284  Unused: 0  Overhead: 192  HeapErrorCode: 0


So the TotalAllocated takes up 12 bytes for the IntToStr function with a small integer value. Then using it again with a larger integer value takes another 4 bytes, and when using it with an even bigger value takes another 4.
0
Comment
Question by:poupou
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 3

Accepted Solution

by:
sperling earned 50 total points
ID: 1337834
This is not a leak, this is just a side-effect of delphi's long string handling. Try putting the above code in another procedure. Then display TotalAllocated before and after a call to the procedure, and you'll see that the amount of allocated memory is the same. Also, try with a loop performing e.g. 10000 IntToStr on a random value, and you will se a very low difference in total allocated memory before and after the loop. Again, when the procedure exits, that memory is automatically deallocated.

If this was a leak, the memory indirectly allocated by IntToStr would never have been freed. As exiting the procedure frees it, it is not a leak.

Regards,

Erik.

0
 

Author Comment

by:poupou
ID: 1337835
Sorry Erik, but putting the IntToStr code into a procedure and checking TotalAllocated before and after it does not change anything.
Even with the latest system.dcu download from Borland.
At least its true that the memory "leak" do not grow with the number of calls to that function.

Another guy told me the memory was released after some time. And its true that the memory is released after some time (but not only after a call to the sleep function !).
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question