   Bad pointers in class destructors? (C++)
  Hi I am new here, and as per most new users (I assume), am in need information/advice.I am most of the way through making a game using SDL in c++, however have run into a bit of a wall, that I need some help with, if anyone is willing.Using SDL_Mixer, I decided to put the Mix_Chunk pointers in the classes for the enemy/player that will need the sound effects. ie.in the class declaration file I put something likeClass Player{private:Mix_Chunk* soundeffect;}Then in the source file for the classesPlayer::Player(){soundeffect = Mix_LoadWAV("effectfile.wav");}I then decided that, in order to avoid a memory leak I should have a custom destructor:Player::~Player(){Mix_FreeChunk(soundeffect);}Now, this compiles fine, but causes an error when it runs. Upon debug the program inserts a break at the Mix_FreeChunk() line, saying that soundeffect is a bad pointer...any idea on why this is happening? (Sorry for not posting actual code, there is a lot of other stuff in the classes that probably won't be useful). If there is any other information you need, please ask.Thanks in advance for any suggestions   
  Few quick things you could check, 1, Mix_LoadWAV is not actually loading the file because maybe it doesnt exist? and is returning a null pointer, which Mix_FreeChunk maybe cant handle?. 2, you are destroying "soundeffect" somewhere else and leaving it dangling in the class.You did say you decided to have a custom destructor at some point, so check where you were calling Mix_FreeChunk before this destructor went in.   ;  Since you're new to the forums, a quick tip: you can use [ source ] tags to put your code in one of those nice little code boxes (which, in general, will make it easier to read).Although there are some possible design issues there, I don't see anything inherently wrong with your example (other than that 'class' is capitalized). Are you sure the sound effect is loading correctly? Are you able to play back the sound effect successfully during the game? Is the 'player' object created after SDL_mixer is initialized? Is it destroyed before SDL_mixer is shut down? Are you making a copy of the player object at any point? Can you post the exact error message that you're getting?Any other information you can provide would be helpful. If you're not able to find the cause of the problem, a typical next step would be to reduce the program to a minimal working example that demonstrates the error behavior. Quite often you'll find the problem during this process, but if you don't, you'll then have an example program that (presumably) will be short enough to post to the forum in its entirety, at which point other people will be able to take a look at it and perhaps help you find the cause of the error.   ;  Another probably way to have Mix_FreeChunk called earlier is if the class doesn't have a proper copy constructor/assignment operator and gets copied/assigned somewhere. (You can declare them private to see if they are used anywhere.)I suppose boost::shared_ptr would be able to look after the resource:Class Player{private:    boost::shared_ptr<Mix_Chunk> soundeffect;}Then in the source file for the classesPlayer::Player(){    Mix_Chunk* mix = Mix_LoadWAV("effectfile.wav");    if (mix) { //if it returns NULL on failure   soundeffect = boost::shared_ptr<Mix_Chunk>(mix, Mix_FreeChunk);    }    else {   throw failed_to_load_resource;    }}Player::~Player(){    //nothing here}Another thing, which is good for memory leak avoidance, is to wrap resource-management into lower level classes (like boost::shared_ptr, or your own special-purpose class). High-level classes like Player shouldn't be concerned with such mundane tasks.   ;  Ok, well I had checked already checked the file, but I just did again to make sure, and if I use it outside of the class then the program can load and play the file just fine. And I haven't called Mix_FreeChunk anywhere else apart from in that destructor, so I don't think it can be getting destroyed before the destructor is called.jyk thanks for the tip about the source tags, I will do this in future. The player object is definitely called after SDL_Mixer is initialised. I looked for when SDL_mixer is closed and realised that (in a huge mistake) I had created a clean up function to close SDL_Mixer but never called it, so that wasn't the problem (although it was obviously another problem!)There shouldn't be a copy of the player anywhere as far as I know.the exact error message is:"Unhandled exception at 0x770159c3 in Platformer.exe: 0xC0000005: Access violation reading location 0xdbc8cecd."   ;  Ok, sorry for the double post, but I just ran the program again after commenting out the code in the destructor, and noticed that one sound effect (jump) works perfectly, whereas the other doesn't work atall (no noise is heard) even though the way I have used them is identical...   ;  You stated that you didn't copy Player objects, but the compiler may have.Among c++ programmers there is something known as the rule of three.That rule states that when you need one of the three following functions, you must provide all three of them:destructorcopy constructorassignment operator(with a const reference to the same class)   ;  Try giving Player a private copy constructor. If it doesn't compile after that then you're probably making a copy that you don't know of.   ;  Do I need any specific code in the copy constructor?   ;  Mix_FreeChunk and/or Mix_FreeMusic (I forget which) causes an exception when you pass it a NULL pointer. SDL_FreeSurface, TFF_CloseFont, at pretty much any other such functions allow NULL pointers just fine; Mix_FreeChunk/Mix_FreeMusic does not. (Guess how I found that out? [grin])So beforehand, check if your Mix_Chunk is NULL.if(soundeffect)Mix_FreeChunk(soundeffect);Another bug that you'll probably run into soon: Do not use MP3s with SDL_mixer. Use Ogg files instead. Why? Because Mix_PlaySong() randomly, and I do mean randomly, crashes when you play a MP3.In my previous game project, I isolated a crash coming from Mix_PlayMusic. It'd occur when I'd play a Mix_Music loaded from a MP3, and it would be inconsistent in its crashing. Sometimes it'd work fine for the first 20 or 30 plays, and then spontaneously crash, and sometimes it'd crash the very first time I'd play it. I isolated it as far down as I could, and it definitely wasn't anything I was doing, it was Mix_PlayMusic. Others online have experienced this bug as well - the solution is to use Ogg. I'm not aware if a more recent version of SDL_Mixer fixed it.   