Recent

Author Topic: Memory leaks in copy function  (Read 2021 times)

ezikoh

  • New Member
  • *
  • Posts: 32
Memory leaks in copy function
« on: March 24, 2017, 04:23:26 pm »
Hello

I use the tools Leaks and Traces of lazarus, and say what exists memory leak in function copy.

I have the statement

 executeCmd:=copy(dataString,2,(LastDelimiter('&&',dataString)-3));

where executeCmd is name of function returning a string, dataString is a concatenate of two preview strings.

I not understand why exists a memory leaks.
Also too show memeroy leak in the statement 

 executeCmd:=copy(dataString,2)

Any idea?, thank you.




Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9754
  • Debugger - SynEdit - and more
    • wiki
Re: Memory leaks in copy function
« Reply #1 on: March 24, 2017, 05:33:39 pm »
From that bit of code... Impossible to tell....

But keep in mind, heaptrc tells you:
- where the memory was allocated
- NOT where it was leaked.

just an example

myObj.fname = copy('foo',1);

If I later leak myObj, then the above line will be in the leak traces. Because the memory for 'f' was allocated at this point. But the leak happens somewhere else.

So if you have more leaks in the list, go through all the leaks you have. See if any of the others might have a reference to the result.
That is what do you do with the function result, do you store it? Maybe that stored result (or ANY later (full) copy of / reference to it) are leaked....

Thaddy

  • Hero Member
  • *****
  • Posts: 14159
  • Probably until I exterminate Putin.
Re: Memory leaks in copy function
« Reply #2 on: March 24, 2017, 06:18:45 pm »
I can't reproduce this with simple code like this and compiled with fpc -glh project.lpr
Code: Pascal  [Select][+][-]
  1. program project1;
  2. {$mode objfpc}{$H+}
  3. uses sysutils;
  4. var
  5.   executeCmd,datastring:string;
  6. begin
  7.   datastring :='aa test me a && bit';
  8.   //executeCmd:= copy(dataString,4);
  9.   executeCmd:= copy(dataString,2,(LastDelimiter('&&',dataString)-3));
  10.   writeln(executeCmd);
  11.   readln;
  12. end.

No leaks in both cases. Must be something else...
Specialize a type, not a var.

ezikoh

  • New Member
  • *
  • Posts: 32
Re: Memory leaks in copy function
« Reply #3 on: March 24, 2017, 06:39:42 pm »
Thanks for the answers.

I have a list of memory leaks, sure that is originated for another reason and not in the copy function as it marks me, as he said Martin_fr.

I'm going to check the data and variables.

Thanks for the help.

 

TinyPortal © 2005-2018