* * *

Author Topic: Many memory leaks not in my code  (Read 1541 times)

russdirks

  • New member
  • *
  • Posts: 35
Many memory leaks not in my code
« on: April 18, 2017, 06:23:49 am »
I recently enabled heaptrc for a program I am working on.  I used the project options screen: -gh as well as -gl.  I did not manually add the unit to my code.  I found it very helpful to locate a number of memory leaks.  However, the heap.trc file shows many, many memory leaks that contain no line number information.  Does that mean the leaks are in pascal system code?  Here is the first few entries, after running my program for only 2 minutes.

Code: [Select]
Heap dump by heaptrc unit
69136 memory blocks allocated : 34568850/34824576
68448 memory blocks freed     : 34527568/34782368
688 unfreed memory blocks : 41282
True heap size : 1474560
True free heap : 1326080
Should be : 1344288
Call trace for block $0000000006A1AF50 size 43
  $0000000100011282
  $00000001000097F3
  $000000010000A84C
  $000000010000D91A
  $000000010015F750
  $000000010015E0D5
  $000000010015D353
  $BAADF00DBAADF00D
Call trace for block $0000000006A2D9F0 size 16
  $0000000100011282
  $000000010000E427
  $000000010015F732
  $000000010015E0D5
  $000000010015D353
  $000000010015D670
  $000000010015D397
  $BAADF00DBAADF00D
Call trace for block $0000000006A2D950 size 16
  $0000000100011282
  $000000010000E427
  $000000010016027A
  $000000010015DF6E
  $000000010015D4D9
  $000000010015D3D9
  $000000010015D670
  $BAADF00DBAADF00D

How can I debug this?


russdirks

  • New member
  • *
  • Posts: 35
Re: Many memory leaks not in my code
« Reply #1 on: April 18, 2017, 07:54:27 am »
I should  mention I am getting these symptoms on both Windows 10 and OSX

molly

  • Hero Member
  • *****
  • Posts: 1799
Re: Many memory leaks not in my code
« Reply #2 on: April 18, 2017, 08:06:51 am »
Many memory leaks not in my code
That is what they all say  :P

Usually that means you've done something that is buried away and heaptrace was not able to directly trace it back to your code.

The trace as shown does not indicate with certainty that it is not directly related to your code. Of course the opposite is also true in that it might be a 3th party class/component that is causing the trace or perhaps you stumbled upon a bug that is present inside rtl.

Compile your rtl with debug and gl turned on and you should be able to get a better trace. See also this thread

Quote
$BAADF00DBAADF00D
That isn't something pretty, rather an indication something went seriously wrong as it is a special marker reading "Bad Food"

PS: in case the number of unfreed memory blocks goes higher the longer you run your program then think about where in your code you add something to f.i. a (object)list, treelist or similar and you forgot to free the items that you added over time.
« Last Edit: April 18, 2017, 08:21:40 am by molly »

Eugene Loza

  • Sr. Member
  • ****
  • Posts: 449
    • My "almost daily" development blog
Re: Many memory leaks not in my code
« Reply #3 on: April 18, 2017, 08:59:26 am »
Try RUN>CLEAN UP AND BUILD and then run again. Helped me two days before :)
And yes, don't repeat my errors. Always have debug mode on and track memory leaks unless you do a release. It's much easier to fix them as soon as you see them (when you know what exactly you've added/changed), than trying to fix memory leaks in old code (the exact thing I've been doing at weekends :))
My Free and Open Source games in Lazarus/FreePascal/CastleGameEngine:
https://decoherence.itch.io/
(and some ancient games in Turbo Pascal too)
Sources are here: https://github.com/eugeneloza?tab=repositories

Thaddy

  • Hero Member
  • *****
  • Posts: 4423
Re: Many memory leaks not in my code
« Reply #4 on: April 18, 2017, 11:18:00 am »
Quote
$BAADF00DBAADF00D
That's called a marker..., like the more commonly known $DEADBEEF.  8-) It's just more friendly to vegetarians. (or not.., I like dead beef.)
.
In effect it tells you your code s*cks. $FADFED

Anyway: in gdb: go to the address, set a break point there, you know what leaks.
People who can't debug should refrain from programming.<grumpy mode on  >:D >:D >:D >:D>

Hmm, can anybody think of anything better to insult - slightly - within the constraints of A..F? (DEADDAD doesn't count))
« Last Edit: April 18, 2017, 12:08:48 pm by Thaddy »
"Logically, no number of positive outcomes at the level of experimental testing can confirm a scientific theory, but a single counterexample is logically decisive."

MathMan

  • Full Member
  • ***
  • Posts: 149
Re: Many memory leaks not in my code
« Reply #5 on: April 18, 2017, 02:42:19 pm »
Quote
$BAADF00DBAADF00D
That's called a marker..., like the more commonly known $DEADBEEF.  8-) It's just more friendly to vegetarians. (or not.., I like dead beef.)
...
Hmm, can anybody think of anything better to insult - slightly - within the constraints of A..F? (DEADDAD doesn't count))

Hi Thaddy - how about "BADDEED5"? - ok it's not 100% in A..F but 5 is similar enough to S I'd say ...

Leledumbo

  • Hero Member
  • *****
  • Posts: 7649
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Many memory leaks not in my code
« Reply #6 on: April 18, 2017, 02:44:02 pm »
The leak doesn't always lie your code, but the way your code uses the RTL/FCL/whatever that's compiled without debug info. In that case, the best the RTL can do is to print the stack traces only.

@Thaddy:
Grab from https://en.wikipedia.org/wiki/Hexspeak

russdirks

  • New member
  • *
  • Posts: 35
Re: Many memory leaks not in my code
« Reply #7 on: April 18, 2017, 06:11:07 pm »
Many memory leaks not in my code
That is what they all say  :P

Yeah, I probably shouldn't have been so confident.  I may have to eat my words.  But that's a lot better than eating $BAADF00DBAADF00D:D  (Sorry, couldn't resist)

Quote
Compile your rtl with debug and gl turned on and you should be able to get a better trace.


Recompiling right now, using the "Build Lazarus : Debug IDE" profile which I assume is how you do what you are referring to.

russdirks

  • New member
  • *
  • Posts: 35
Re: Many memory leaks not in my code
« Reply #8 on: April 18, 2017, 06:27:46 pm »
Quote
Recompiling right now, using the "Build Lazarus : Debug IDE" profile which I assume is how you do what you are referring to.

Ok, that didn't work.  I'm still not getting line numbers in the stack traces.  How exactly do I "Compile the rtl with debug and gl turned on"?

russdirks

  • New member
  • *
  • Posts: 35
Re: Many memory leaks not in my code
« Reply #9 on: April 18, 2017, 07:06:52 pm »
Ok, I found the problem, the old fashioned way ... going through my code line by line.    :-[

GetMem

  • Hero Member
  • *****
  • Posts: 2248
Re: Many memory leaks not in my code
« Reply #10 on: April 18, 2017, 07:15:16 pm »
You most likely use some kind of composite array/list in your program.  Just before you free the list make sure you also free every object(s) belonging to each item. You need some kind of Finalize(Data) where Data is a pointer.  I'm I right?

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 4333
    • wiki
Re: Many memory leaks not in my code
« Reply #11 on: April 18, 2017, 07:15:59 pm »
Just a quick note: Not sure what OS and which version of fpc you use.

But there was some report that fpc 3.0.2 has a regression on Linux (not sure 32 or 64 bit or even both?). In some cases line numbers are not shown, despite the compiler setting used, would normally show them.

Check the mailinglist and/or the bugtracker

russdirks

  • New member
  • *
  • Posts: 35
Re: Many memory leaks not in my code
« Reply #12 on: April 18, 2017, 07:20:09 pm »
You most likely use some kind of composite array/list in your program.  Just before you free the list make sure you also free every object(s) belonging to each item. You need some kind of Finalize(Data) where Data is a pointer.  I'm I right?

No, actually it was the GetJSON function.  It converts a JSON string to an object, which you are responsible to free, but I wasn't doing that.  The documentation should really say something about that, although it is kind of obvious.
« Last Edit: April 18, 2017, 07:22:35 pm by russdirks »

molly

  • Hero Member
  • *****
  • Posts: 1799
Re: Many memory leaks not in my code
« Reply #13 on: April 19, 2017, 10:43:31 am »
Quote
Recompiling right now, using the "Build Lazarus : Debug IDE" profile which I assume is how you do what you are referring to.

Ok, that didn't work.  I'm still not getting line numbers in the stack traces.  How exactly do I "Compile the rtl with debug and gl turned on"?
Compiling the IDE in DEBUG mode should do the trick.

However, do note the remark made by Martin_fr. I was not aware, so apologies. On the other hand you was very vague with mentioning target, versions and what code was causing you issues.

Quote
No, actually it was the GetJSON function.  It converts a JSON string to an object, which you are responsible to free, but I wasn't doing that.  The documentation should really say something about that, although it is kind of obvious.
Ah, yes. GetJSon will do that for you. Indeed obvious but something easily overlooked.

Glad you where able to locate the culprit.

It is usually a pita to find something like that back when your project has grown to substantial size and offending code is tucked away deep inside your project.

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus