Skip to main content
gamedev.net
🔒 Locked

Memory corruption/leak problem

Started by XTAL256 Mar 23, 2009 at 5:11 AM 7 replies 3.1k views
Original Post
XTAL256
XTAL256
I just wrote some code to copy an image (using SOIL) and i got a strange error:
Windows has triggered a breakpoint in Ballistic.exe.

This may be due to a corruption of the heap, and indicates a bug in Ballistic.exe or any of the DLLs it has loaded.

The output window may have more diagnostic information
Here is the code:

Image::Image(const Image& copy) {
    this->width = copy.width;
    this->height = copy.height;

    // Copy texture data and create a new texture
    unsigned char* temp = new unsigned char[width*height*4];
    glBindTexture(GL_TEXTURE_2D, copy.texId);
    glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, temp);
    this->texId = SOIL_create_OGL_texture(temp, width, height,
                    SOIL_LOAD_RGBA, SOIL_CREATE_NEW_ID,
                    SOIL_FLAG_TEXTURE_REPEATS);

    //### Error happens in here ###
    SOIL_free_image_data(temp);  // Delete temporary data
}


If i comment out SOIL_free_image_data to see if the image is copied correctly then i get another error somewhere else in my code (displaying some text somewhere):
Unhandled exception at 0x7c812aeb in Ballistic.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0012f698..
That doesn't sound good but it means that the problem is not localised to some piece of code, there must be a memory leak somewhere or something. Quite a while ago, then i was working on the GUI for my game, i discovered that i was getting a null pointer exception when creating one of the windows (not an OS window, just one of my objects). It was a static class variable that was used for the text font. This variable was initialised earlier and shouldn't have been deleted, so i figured it was a memory problem. I put it on the back-burner until i had time to fix it. Well i have a feeling that it might be causing this too. Of course, i don't expect anyone to fix it for me, but i was wondering if there are any ways to try and target where the cause of the problem is. I can use Visual Studio's debugger to see the value of the font variable at points in the execution of the program but i can't see where it is being changed. Basically, i am posting here as a last resort. I've tried nothin' and i'm all out of ideas! (that was from The Simpsons [grin]) thanks
[Window Detective] - Windows UI spy utility for programmers
14 Molgash Avenue
14 Molgash Avenue
Hi, it might be worth checking your program with MS App Verifier (http://msdn.microsoft.com/en-us/library/ms220948(VS.80).aspx). It will dbg break on the line causing memory corruption, although it's likely to be in a library where you don't have the source code. :)
Codeka
Codeka
Quote:
Original post by XTAL256
unsigned char* temp = new unsigned char[width*height*4];
// ...
SOIL_free_image_data(temp);
You're allocating memory with operator new[] and trying to free it with SOIL_free_image_data That doesn't seem right to me: you should always use the corresponding method to free resources: operator new[] + operator delete[].
Antheus
Antheus
Quote:
Original post by XTAL256
unsigned char* temp = new unsigned char[width*height*4];

Quote:
/**
Frees the image data (note, this is just C's "free()"...this function is
present mostly so C++ programmers don't forget to use "free()" and call
"delete []" instead [8^)
**/


This is bad mojo:
x = new foo();free(x);


In your case, just do delete[] temp.
Zahlman
Zahlman
You clean up memory that you allocate. APIs clean up memory that APIs allocate. You tell APIs to clean up memory that you told the API to allocate. Everyone's happy. Yes? :)
XTAL256
XTAL256
Quote:
Original post by Codeka
You're allocating memory with operator new[] and trying to free it with SOIL_free_image_data

Ah, yes. Thanks for pointing that out Codeka. I thought something smelled fishy when it triggered the breakpoint in that call (it was actually inside the free() function that SOIL_free_image_data calls). The only reason i used that method is because SOIL says to use it to clean up any image memory. But, SOIL wasn't the one who allocated it, so your right, i should use delete[] [smile].
thanks

I'm not sure if that will fix that problem but it's a start. I will also have a look at MS App Verifier.
[Window Detective] - Windows UI spy utility for programmers
Zahlman
Zahlman
That said, if you're passing the memory to a SOIL function, maybe it would be better to let SOIL allocate it? :)
XTAL256
XTAL256
I replaced SOIL_free_image_data with delete[] and i still get the error. I use the exact same code to copy each frame of an animation and that has worked. So the problem is probably not with SOIL but due to the nature of the problem it really could be anywhere.
Quote:
although it's likely to be in a library where you don't have the source code

Actually i have the code for both this library and the lib i use for parsing XML. Yay for Open Source software [smile].

Quote:
Original post by Zahlman
That said, if you're passing the memory to a SOIL function, maybe it would be better to let SOIL allocate it? :)

Na, that's not what i'm doing here. SOIL_create_OGL_texture takes a pointer to an array of image data already in memory (so i have to create it / have it already created) and creates an OpenGL texture from it. I get that data from an existing texture using glGetTexImage. This is how i copy the image. If you have a better way of copying OpenGL textures then i'd like to hear it. (and i'm not being sarcastic, the code i use is kind of a hack [grin])
[Window Detective] - Windows UI spy utility for programmers
XTAL256
XTAL256
I just ran Application Verifier and had a look at the logs. It seems to be telling me that there was an exception before my game even started (somewhere in SDL_main, oh yeah, i'm using SDL). It normally at least starts up, i only get the error after i click a button on my GUI, i.e. it has finished loading code and entered the event loop.
Here is the log if anyone knows what to make of it:
<?xml version="1.0" encoding="UTF-8" standalone="no"?><avrf:logfile xmlns:avrf="Application Verifier">    <avrf:logSession TimeStarted="2009-03-24 : 21:01:04" PID="1660" Version="2">        <avrf:logEntry Time="2009-03-24 : 21:01:05" LayerName="Memory" StopCode="0x60D" Severity="Error">            <avrf:message>Unexpected exception raised in DLL entry point routine.</avrf:message>            <avrf:parameter1>1578fe0 - DLL name (use du to dump it).</avrf:parameter1>            <avrf:parameter2>12e87c - Exception record. Use .exr to display it.</avrf:parameter2>            <avrf:parameter3>12e898 - Context record. Use .cxr to display it.</avrf:parameter3>            <avrf:parameter4>31f4fd8 - Verifier dll descriptor</avrf:parameter4>            <avrf:stackTrace>                <avrf:trace>vfbasics!AVrfpDllEntryPointExceptionFilter+73</avrf:trace>                <avrf:trace>vfbasics!AVrfpStandardDllEntryPointRoutine+133</avrf:trace>                <avrf:trace>ntdll!LdrInitializeThunk+24</avrf:trace>                <avrf:trace>ntdll!LdrFindResourceDirectory_U+28d</avrf:trace>                <avrf:trace>ntdll!RtlValidateUnicodeString+507</avrf:trace>                <avrf:trace>ntdll!LdrLoadDll+110</avrf:trace>                <avrf:trace>vfbasics!AVrfpLdrLoadDll+49</avrf:trace>                <avrf:trace>kernel32!LoadLibraryExW+c8</avrf:trace>                <avrf:trace>kernel32!LoadLibraryW+11</avrf:trace>                <avrf:trace>OPENGL32!wglSwapMultipleBuffers+93e</avrf:trace>                <avrf:trace>OPENGL32!wglGetProcAddress+cf</avrf:trace>                <avrf:trace>OPENGL32!wglGetPixelFormat+8d</avrf:trace>                <avrf:trace>OPENGL32!wglDescribePixelFormat+97</avrf:trace>                <avrf:trace>OPENGL32!+5ed26da8</avrf:trace>                <avrf:trace>GDI32!ChoosePixelFormat+28</avrf:trace>                <avrf:trace>SDL!SDL_RegisterApp+2795</avrf:trace>                <avrf:trace>SDL!SDL_RegisterApp+4fb1</avrf:trace>                <avrf:trace>SDL!SDL_SetVideoMode+193</avrf:trace>                <avrf:trace>Ballistic!initSDL+16a</avrf:trace>                <avrf:trace>Ballistic!SDL_main+78</avrf:trace>                <avrf:trace>Ballistic!SDL_SetError+2b9</avrf:trace>                <avrf:trace>Ballistic!__tmainCRTStartup+1a6</avrf:trace>                <avrf:trace>Ballistic!mainCRTStartup+d</avrf:trace>                <avrf:trace>kernel32!RegisterWaitForInputIdle+49</avrf:trace>            </avrf:stackTrace>        </avrf:logEntry>    </avrf:logSession></avrf:logfile>
[Window Detective] - Windows UI spy utility for programmers

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.