[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.8

Memory leak when concatenating strings

Asked by sburck in Delphi Programming

Tags: leak, memory, delphi

I'm working on upgrading an old program for an old employer of mine.  The project produces Postscript based on input files (layout files and data files, producing printer-ready ID cards).  The original project was in Delphi 1.0 and Visual C++ 1.5; later on I wrote a Delphi component which does the Postscript generation in Delphi 5, and this was used in subsequent projects.

Now I have been called back to rewrite the front end, using Delphi 5 and the Delphi 5 component (with some minor additions to support Unicode).  During testing, some major memory leaks were found, which apparently have always been there, but never been noticed before.  The new card layout, which uses lots of graphics, has exacerbated the problem.

I downloaded AQTime (http://www.automatedqa.com) (which, at this point, let me highly recommend; I will be buying it when I finally get paid for this project) to search for the source of the memory leaks.  It showed me a few which were simple to solve, but the bulk of them point to this or something like this:

             Fpage.FActualPostscriptData.Strings[FPage.FCurrentIDSide-1] :=
                 Fpage.FActualPostscriptData.Strings[FPage.FCurrentIDSide-1] +
                 TTF.ProducePostscript;

TTF is a method which generates and returns a very large string (it can be several hundred K), and the stringlist Fpage.FActualPostscriptData contains the strings, per ID, of each of the cards).  The leak is actually on the concatenation, where the AQTime shows the concatenation calling to LStrCat3, which calls eventually to VirtualAlloc in GetMem.Inc; and this memory is never freed.

I have tried to move the concatenation to local strings, and made the method function returning a string to a method procedure operating on a string which belonged to the object:

              TTF.ProducePostscript;
              helper := Fpage.FActualPostscriptData.Strings[FPage.FCurrentIDSide-1];
              helper1 := TTF.ProducedPostscript;
              Fpage.FActualPostscriptData.Strings[FPage.FCurrentIDSide-1]  := helper + helper1;

Here, too the leak occurred on the concatenation.  I tried concat(helper, helper1).  No good.  I tried delete and insert.

              TTF.ProducePostscript;
              helper := Fpage.FActualPostscriptData.Strings[FPage.FCurrentIDSide-1];
              helper1 := TTF.ProducedPostscript;
              Fpage.FActualPostscriptData.Delete(FPage.FCurrentIDSide-1);
              Fpage.FActualPostscriptData.Insert(helper+helper1,FPage.FCurrentIDSide-1);

Didn't help.  Tried using a dynamic array to avoid the concatenation:

var
      copier : array of char; (this is local to the procedure doing this)
...
             SetLength(Copier,
                  Length(Fpage.FActualPostscriptData.Strings[FPage.FCurrentIDSide-1])
                + Length(TTF.ProducedPostscript));
             Move(Fpage.FActualPostscriptData.Strings[FPage.FCurrentIDSide-1][1],Copier[0],
                Length(Fpage.FActualPostscriptData.Strings[FPage.FCurrentIDSide-1]));
             Move(TTF.ProducedPostscript[1],
                  Copier[Length(FPage.FActualPostscriptData.Strings[FPage.FCurrentIDSide-1])],
                  Length(TTF.ProducedPostscript));
             SetLength(helper,Length(Copier));
             Move(Copier[0],helper[1],Length(Copier));
             SetLength(Copier,0);
             Copier := nil;
             FPage.FActualPostscriptData.Strings[FPage.FCurrentIDSide-1] := helper;
             helper := '';

Now AQTime shows the memory of the first SetLength(Copier, etc.) as unreleased, despite the SetLength(Copier,0) and Copier := nil.  It's actually worse than the string version.

I am at a loss here - what could cause these leaks?
[+][-]01/14/07 02:12 PM, ID: 18313001Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zone: Delphi Programming
Tags: leak, memory, delphi
Sign Up Now!
Solution Provided By: Ivanov_G
Participating Experts: 4
Solution Grade: A
 
[+][-]01/14/07 02:14 PM, ID: 18313014Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01/14/07 06:10 PM, ID: 18313688Assisted Solution

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 30-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]01/14/07 09:14 PM, ID: 18314287Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01/15/07 12:18 AM, ID: 18314929Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01/16/07 01:31 AM, ID: 18322448Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01/16/07 02:28 AM, ID: 18322623Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01/16/07 03:35 AM, ID: 18322905Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01/16/07 04:20 AM, ID: 18323153Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01/16/07 04:42 AM, ID: 18323290Assisted Solution

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 30-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]03/12/07 01:03 PM, ID: 18705299Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03/12/08 01:24 PM, ID: 21110371Administrative Comment

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 30-day free trial to view this Administrative Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-92