Tracking down memory allocation error

Locked
Kaz
Posts: 1077
Joined: Wed Mar 08, 2006 3:43 am

Tracking down memory allocation error

Post 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!
^misantropia^
Posts: 4022
Joined: Sat Mar 12, 2005 6:24 pm

Re: Tracking down memory allocation error

Post by ^misantropia^ »

Have you tried running it through valgrind?
^misantropia^
Posts: 4022
Joined: Sat Mar 12, 2005 6:24 pm

Re: Tracking down memory allocation error

Post 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?
Kaz
Posts: 1077
Joined: Wed Mar 08, 2006 3:43 am

Re: Tracking down memory allocation error

Post 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?
Last edited by Kaz on Thu Apr 04, 2013 2:39 am, edited 1 time in total.
^misantropia^
Posts: 4022
Joined: Sat Mar 12, 2005 6:24 pm

Re: Tracking down memory allocation error

Post 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.
Kaz
Posts: 1077
Joined: Wed Mar 08, 2006 3:43 am

Re: Tracking down memory allocation error

Post 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:
^misantropia^
Posts: 4022
Joined: Sat Mar 12, 2005 6:24 pm

Re: Tracking down memory allocation error

Post by ^misantropia^ »

I assume you use Visual Studio? If you compile with `gcc -Wall`, it'll complain about stuff like this.
Kaz
Posts: 1077
Joined: Wed Mar 08, 2006 3:43 am

Re: Tracking down memory allocation error

Post by Kaz »

I'm using Mingw with eclipse
Locked