Solved

memory leak in Delphi2.01 with basic functions

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi XE10 Round Image 2 127
How to define IfThen functions in one common unit? 4 52
Multi-layered image in FireMonkey 9 34
Twebbrowser add css to the header 3 25
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

770 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