Page 1 of 1

Tracking down memory allocation error

Posted: Sun Oct 17, 2010 10:16 pm
by Kaz
I've been writing an ASE model loader for a couple of weeks now, and a single bug has eluded me for almost a week. I'm not exactly positive what is causing it, so I'll just provide the clues I'm aware of, and see if anyone with more experience can help:
  • I'm using plain ANSI C
  • Here is the bulk of relevant source code, and you can see everything else up there as needed: http://github.com/freemancw/Exact3D/blo ... odel_ase.c
  • Here is a test ASE file that produces the bug: http://student.cs.appstate.edu/freemancw/junk/test.ASE
  • If I run the code inside of eclipse with the gdb debugger, it works fine. If I run the executable, it crashes.
  • I have debugging code that prints out the values of all of the fields of a given model. For test.ASE, the problematic area produces the following.

    Code: Select all

    Face Count: 2 <- this is correct
    =======================================================
    Face ID: 2 <-- should be 0
    A: 8, B: 2, C: 7404232, AB: 7372608, BC: 7404352, CA: 7404504 <- all of these values are wrong
    Smoothing Group: 1 <- from here on everything is correct
    Material ID: 0
    Face Normal: X = 0.000000, Y = 0.000000, Z = 1.000000
    Face ID: 1
    A: 1, B: 3, C: 0, AB: 1, BC: 0, CA: 1
    Smoothing Group: 1
    Material ID: 0
    Face Normal: X = 0.000000, Y = 0.000000, Z = 1.000000
    
As of the moment I'm thinking that it's reading from some random area of the heap, but I'm really at a loss of how I should go about tracking this down. The fact that it works in the debugger but not by just running the executable seems pretty significant to me, but I'm not experienced enough to know why. Thanks for any help!

Re: Tracking down memory allocation error

Posted: Sun Oct 17, 2010 10:23 pm
by ^misantropia^
Have you tried running it through valgrind?

Re: Tracking down memory allocation error

Posted: Sun Oct 17, 2010 10:29 pm
by ^misantropia^
Okay, did a quick check. The culprit seems to be at line 131:

Code: Select all

materialList.materialList[currentMaterialID].materialID = currentMaterialID;
EDIT: materialList.materialList is a pointer and you never malloc()'ed memory for it. Where can I send my invoice to?

Re: Tracking down memory allocation error

Posted: Sun Oct 17, 2010 10:38 pm
by Kaz
No sir, I should also mention I'm running on Win7. Which would be the best tool to check out, maybe IBM's Purify?

edit: haha, not so fast! are you looking at the correct file?

Re: Tracking down memory allocation error

Posted: Sun Oct 17, 2010 11:29 pm
by ^misantropia^
Ah wait, wrong branch.

Line 282: you realloc() without assigning the result. Pro tip: you don't need to call malloc() separately, realloc(NULL) does the same thing.

As to purify, it's pretty good but not nearly as thorough as valgrind is. Valgrind runs your code in a kind of emulator that allows it to track every instruction and every byte read or written. It's truly awesome.

Re: Tracking down memory allocation error

Posted: Sun Oct 17, 2010 11:45 pm
by Kaz
KAAAAAHHHHNNN!!!

Thanks a million, I'm pretty sure that solved my problem! For some reason I wasn't thinking that realloc returned a pointer.... dunno why :shrug:

Re: Tracking down memory allocation error

Posted: Mon Oct 18, 2010 8:56 am
by ^misantropia^
I assume you use Visual Studio? If you compile with `gcc -Wall`, it'll complain about stuff like this.

Re: Tracking down memory allocation error

Posted: Tue Oct 19, 2010 1:30 am
by Kaz
I'm using Mingw with eclipse