-2

I'm making an app using SDL 2. I just recently started using the SDL_Mixer library and this has brought about an issue for me. Forgive me if my explanation seems vague, as this bug is extremely confusing.

Sometimes, when closing out of my program, the program crashes. Only sometimes this happens. After removing parts of code, I found that the line causing this was a simple delete call on a pointer of type Window*. If I remove that delete call, no more crashes happen when I close the program. Another thing that's important to note is that the crash occurs right after delete is called, meaning the program doesn't even go inside of ~Window() which is really strange.

Something else I noticed was that when I remove the Mix_OpenAudio(...) call from the beginning of the program, no crashes happen at all, regardless of whether or not the delete call is there. So initializing SDL_Mixer has something to do with the crashes, but this just adds more confusion.

I'm hoping that someone has an idea of what could be the problem here, but if my explanation is too vague then I'll try to replicate the problem into a small enough segment of code that I can post here. I didn't want to copy my entire codebase onto this question because I don't want the mods to hate me. Any ideas?

Coffee Maker
  • 1,543
  • 1
  • 16
  • 29

3 Answers3

0

Without seeing the code in question, here is my guess at the two most likely causes:

  1. Your Window * pointer is uninitialized or points to an illegitimate memory address.
  2. The Window referenced through that pointer has already been deleted, and you crash on trying to delete it a second time. This tends to crash before you get into the destructor, because what the Window* points to isn't valid memory any more and doesn't have a vtable with the pointer to the destructor.
Crashworks
  • 40,496
  • 12
  • 101
  • 170
0

This is mostly an example of undefined behavior which is a particular condition that, when reached, doesn't have any specific guarantees about what will happen.

Deleting an invalid pointer is indeed undefined behavior. Now the problem is that you must understand why and when the pointer to Window becomes invalid, some hints:

  • pointer is uninitialized
  • pointer was already been deleted
  • pointer is the result of another operation which brings undefined behavior (like dereferencing a member of an invalid pointer)
  • pointer was stored inside a local/stack variable which is not valid anymore when you call delete (so you are not actually not trying to delete the correct pointer at all, eg returning a reference to a pointer stored in a local variable from a method)
Jack
  • 131,802
  • 30
  • 241
  • 343
0

I had to test before I answered your question, but looks like my memory was correct: If you call delete more than once on a pointer, your program will crash. So I think the pointer you're deleting was already deleted.

Here's my test: Running a simple test with g++

I tried setting p to null after deleting it, and that made the following if succeed. But if I commented out the part that sets p to null, the if treated p as true, and the program crashed.

enter image description here

You need a way to test and see if the pointer is null or not before you delete it. Unfortunately I don't know how to test a pointer to see if it's pointing to valid data. I thought a simple "if (pointer)" would work, but as you can see from my last image, it doesn't.

Magical Gordon
  • 404
  • 2
  • 8