Error when closing the window in Debug Mode


#1

When I try to close the window, this message appears:

.
This only happens in the Debug mode. I’m using right know the Visual Studio Enterprise, and I compiled the source code with Cmake.

I’m basically just drawing some rectangles, lines, points, and at the end, I call the necessary to free things and set pointers to NULL when the application finishes .How do I resolve this?


#2

A little after making the post I think I discovered the reason of the error. I’m basically destroying a texture that is already NULL, could someone explain why this causes an assertion failure?


#3

You are trying to delete memory thats already ready deleted or doesn’t exist. This will cause an unexpected result or segmentation fault.

In debug mode the compiler does not optimize your code and includes the debug info which is probably catching this issue.

You need to make sure you only delete the pointer once and only once.

// Replicate your issue

int main() {
    int* x = new int(10);
    delete x;
    delete x;
}

// Fix your issue
There are many ways to fix this issue,
#1 For every new you create you have the same amount deletes.
#2 Using smart pointers if your using c++
#3 Check before deleting

int main() {
    int* x = new int(10);
    if (x != NULL) {
        delete x;
    }
}

#4

Calling delete (or free()) on NULL is totally valid and does nothing.

However, calling SDL_DestroyTexture() (and probably other similar SDL functions) is an error.


#5

What I don’t understand is the reason it only happens in the Debug mode, shouldn’t it happens in Release mode as well?


#6

calling them with NULL is an error, sorry forgot that part of the sentence

maybe SDL only does those checks in debug mode and silently ignores this in release mode


#7

Most compilers boil away assert code when not using debug mode.