Solved

memory leak in Delphi2.01 with basic functions

Posted on 1997-06-26
2
366 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
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

746 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now