Quake3World.com Forums
     Programming Discussion
        Tracking down memory allocation error


Post new topicReply to topic
Login | Profile | | FAQ | Search | IRC




Print view Previous topic | Next topic 
Topic Starter Topic: Tracking down memory allocation error

Señor Shambler
Señor Shambler

Joined: 07 Mar 2006
Posts: 849
PostPosted: 10-17-2010 02:16 PM           Profile Send private message  E-mail  Edit post Reply with quote


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:
    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!




Top
                 

Mentor
Mentor

Joined: 12 Mar 2005
Posts: 3957
PostPosted: 10-17-2010 02:23 PM           Profile Send private message  E-mail  Edit post Reply with quote


Have you tried running it through valgrind?




Top
                 

Mentor
Mentor

Joined: 12 Mar 2005
Posts: 3957
PostPosted: 10-17-2010 02:29 PM           Profile Send private message  E-mail  Edit post Reply with quote


Okay, did a quick check. The culprit seems to be at line 131:
Code:
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?




Top
                 

Señor Shambler
Señor Shambler

Joined: 07 Mar 2006
Posts: 849
PostPosted: 10-17-2010 02:38 PM           Profile Send private message  E-mail  Edit post Reply with quote


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 04-03-2013 06:39 PM, edited 1 time in total.

Top
                 

Mentor
Mentor

Joined: 12 Mar 2005
Posts: 3957
PostPosted: 10-17-2010 03:29 PM           Profile Send private message  E-mail  Edit post Reply with quote


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.




Top
                 

Señor Shambler
Señor Shambler

Joined: 07 Mar 2006
Posts: 849
PostPosted: 10-17-2010 03:45 PM           Profile Send private message  E-mail  Edit post Reply with quote


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:




Top
                 

Mentor
Mentor

Joined: 12 Mar 2005
Posts: 3957
PostPosted: 10-18-2010 12:56 AM           Profile Send private message  E-mail  Edit post Reply with quote


I assume you use Visual Studio? If you compile with `gcc -Wall`, it'll complain about stuff like this.




Top
                 

Señor Shambler
Señor Shambler

Joined: 07 Mar 2006
Posts: 849
PostPosted: 10-18-2010 05:30 PM           Profile Send private message  E-mail  Edit post Reply with quote


I'm using Mingw with eclipse




Top
                 
Quake3World.com | Forum Index | Programming Discussion


Post new topic Reply to topic


cron
Quake3World.com
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group