Solved

Getting error: "FastMM has detected a FreeMem call after FastMM was uninstalled"

Posted on 2014-12-04
6
1,723 Views
Last Modified: 2014-12-06
Hi, I'm using FastMM4 in my project and during runtime, it throws off this error:

"FastMM has detected a FreeMem call after FastMM was uninstalled"

I don't use any "Freemem" calls in my code, but I do a "Dispose" in one of my functions which gets called repeatedly from within a loop:

function PhoneNumMask(const Str: string; const hmask: string): PChar;
var
  Pstr, Pmask, ResultStr: PChar;
begin
  Pstr := PChar(Str);
  Pmask := PChar(hmask);
  New(ResultStr);

  while Pmask^ <> #0 do
  begin
    if Pmask^ <> '#' then   // a literal character in the mask, output it directly
       StrCat(ResultStr,PChar(string(Pmask^)))
    else  // output a digit
    begin
      while (PStr^ <> #0) and not (PStr^ in ['0'..'9']) do
        Inc(PStr);
      if PStr^ = #0 then BREAK;  // Exit loop

      StrCat(ResultStr,PChar(string(PStr^)));   // Tack on a digit to the result string
      Inc(PStr);
    end;

    Inc(Pmask);
  end; // while Pmask^ <> #0 do

  Result:=ResultStr;
  Dispose(ResultStr);
end;  // PhoneNumMask

Open in new window


... I prepare a new PChar variable "ResultStr" with the NEW command, then do a DISPOSE of it at the end. I can't see the problem. My app processes text files on disk and I ran a test on a 20 meg file and it went about 5% though the data, and only then gave the error. So the error didn't happen immediately... it took a lot of iterations before it happened.
   Maybe I don't even *need* variable "ResultStr" and I can just assign my function's result to the default "Result"....

Thanks
    Shawn
0
Comment
Question by:shawn857
  • 3
  • 2
6 Comments
 
LVL 19

Expert Comment

by:MerijnB
ID: 40482382
I don't think this piece of code is what triggers the message by FastMM.
You can confirm this simply by commenting out this code (or putting an exit at the first line) and see if the message still appears.

This message is probably triggered when you close your app, not by something you do while running normally.
0
 

Author Comment

by:shawn857
ID: 40482505
Hi Merijn.. well I commented out the FASTMM4 declaration altogether and ran my project without FastMM. It crashed on processing the very first record this time... throwing up simply a "Invalid Pointer operation" error window. I did some tracing in debug and it crashed on this line:

StrCat(ResultStr,PChar(string(PStr^)));   // Tack on a digit to the result string

That line of code had already ran successfully exactly 7 times (it had tacked on 7 digits to "ResultStr" already)... and it was about to tack on another innocent looking digit, the number 9, when it crashed on that line. Strange eh? Could it have something to do with my declaration of variable "ResultStr"? Maybe I need to allocate a fixed amount of space to it or something.. instead of just "New(ResultStr)" ?

Thanks!
    Shawn
0
 
LVL 19

Expert Comment

by:MerijnB
ID: 40482736
I wonder, if you rebuild this using just strings and chars (and skip all the pchars and pointers), does it work as expected?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:shawn857
ID: 40483481
OK I tried a little something here Merijn... I used GetMem and Finalize/FreeMem instead. I also took the "const" out of the function header declaration. See my new function code:

function PhoneNumMask(Str, hmask: string): PChar;
var
  Pstr, Pmask, ResultStr: PChar;
begin
  Pstr := PChar(Str);
  Pmask := PChar(hmask);
  Getmem(ResultStr, length(hmask) + 2);

  while Pmask^ <> #0 do
  begin
    if Pmask^ <> '#' then   // a literal character in the mask, output it directly
       StrCat(ResultStr,PChar(string(Pmask^)))
    else  // output a digit
    begin
      while (PStr^ <> #0) and not (PStr^ in ['0'..'9']) do
        Inc(PStr);
      if PStr^ = #0 then BREAK;  // Exit loop

      StrCat(ResultStr,PChar(string(PStr^)));   // Tack on a digit to the result string
      Inc(PStr);
    end;

    Inc(Pmask);
  end; // while Pmask^ <> #0 do

  StrCat(ResultStr, PChar(chr(13)+chr(10)));
  Result:=ResultStr;
  Finalize(ResultStr);
  FreeMem(ResultStr, length(hmask) + 2);
end;  // PhoneNumMask

Open in new window



On debug, first time through this code it seems to allocate "ResultStr" fine (ResultStr = ''). It goes through the body of the procedure fine and stores a good result in ResultStr (ResultStr = '(308) 444-5555'). I noticed the "finalize(ResultStr)" line does not have a little blue dot in the left margin, so the debugger skips right over that line... odd eh? Then after the Freemem statement executes and immediately after that I check on the contents of ResultStr... it now holds some garbage looking characters. Then I step out back of the function into the next statement following the calling statement. The calling statement was:

OutPhoneNum:= PhoneNumMask(MatchAll[i,0], OutputPhoneFormats[DigitCount]);

... "OutPhoneNum" was declared as a PChar... and there was no "NEW" or "GetMen" statement for it.. should there have been? Anyway,  checking the contents of the returned value in "OutPhoneNum" showed just some garbage characters and not the good phone number I expected. So it appears something is getting "stomped" somewhere along the way.

I didn't try the function yet as using just plain strings and no pointers. I would imagine it would work just fine, but I wanted to use PChar for speed as I am calling this function many times within a tight loop.

Thanks
    Shawn
0
 
LVL 26

Accepted Solution

by:
Sinisa Vuk earned 500 total points
ID: 40484792
My suggestion is to always have pair: try...finally... end with own memory allocated variables. So I modified a little your code - and fix some critical places:
function PhoneNumMask(Str, hmask: string): String;
var
  Pstr, Pmask, ResultStr: PChar;
  nLen: Integer;
begin
  Result:='';
  Pstr := PChar(Str);
  Pmask := PChar(hmask);
  nLen := Length(hmask) + 2 + 1; //zero terminater string

  Getmem(ResultStr, nLen);
  try
    ZeroMemory(ResultStr, nLen);
    while Pmask^ <> #0 do
    begin
      if Pmask^ <> '#' then   // a literal character in the mask, output it directly
         StrCat(ResultStr, PChar(string(Pmask^)))
      else  // output a digit
      begin
        while (PStr^ <> #0) and not (PStr^ in ['0'..'9']) do
          Inc(PStr);
        if PStr^ = #0 then BREAK;  // Exit loop

        StrLCat(ResultStr, PStr, Length(ResultStr)+1);   //1 char - Tack on a digit to the result string
        Inc(PStr);
      end;

      Inc(Pmask);
    end; // while Pmask^ <> #0 do

    StrCat(ResultStr, #13#10);
    Result:=StrPas(ResultStr);
  finally
    FreeMem(ResultStr);
  end;
end;

Open in new window


note: return String rather then Pchar. Main problem is that you allocate memory for result but you free content before exit.
0
 

Author Closing Comment

by:shawn857
ID: 40485167
Sinisa, that worked fantastically... and no more FASTMM4 problems.

Thank you!
   Shawn
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Filemaker 14 vs Delphi Embarcadero 7 186
Copy file in dll not working but working on exe ! 18 88
delphi parse string to params 3 100
SUM 2 INTEGER ARRAYS INTO 1 10 93
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

861 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

30 Experts available now in Live!

Get 1:1 Help Now