"Unhandled exception at..."

Hi.

As the title says, I've started to get an "unhandled exception" error when I EXIT my game, never while playing, to be specific:
"Unhandled exception at 0x698ba051 in NTI-Zombie_Survival.exe: 0xC0000005: Access violation reading location 0x00000008."
That.

What I know, is that it has something to do with an array of 120 slots, that I fill up with pointers to objects - as this is a game, where 120 zombies (in this case) is pretty overkill to start with, I only loop through the first 10 slots when I initialize the game.

My loop for adding the first 10 zombies:
1
2
for(int i = 0; i < 10; i++)
	zombie[i] = new Zombie(zombieTexture);

And I also loop through the whole array before and NULL all slots.

And to add more zombies when necessery:

1
2
3
4
5
6
7
8
9
			if(iKillCounter == iKillsMoreZombies && iNrOfZombies + 3 < 120)
			{
				for(int i = iNrOfZombies; i < iNrOfZombies + 3; i++)
				{
					zombie[i] = new Zombie(zombieTexture);
				}
				iNrOfZombies += 3;
				iKillsMoreZombies *= 1.35;
			}


And to delete:
1
2
3
4
5
6
7
8
for(int i = 0; i < iNrOfZombies; i++)
	{
		if(zombie[i] != NULL)
		{
			delete zombie[i];
			zombie[i] = NULL;
		}
	}


If I use:
delete [] zombie;
to delete the array, I get another error:
http://bayimg.com/pAomhAAdE

I'm using SFML for all the graphics and input handling and such, if that makes any difference.

So, any ideas on how to solve this?
Tell me if there is anything more you need to know, at least I think I've included everything that's important.

Thanks in advance.
Try switching your dynamic array of zombies to a std::vector, which is a re-sizable array. It is much easier to use, more readable and safer.

http://cplusplus.com/reference/stl/vector/
ModShop:
Since I've tried to replace my array with a vector earlier, but didn't really get it working that time;
If I have a vector called zombies and call zombies.erase(zombies.begin + 3); (onyl as an example), will it call delete for me, or do I have to call delete before I erase it from the vector?

Sorry if this is a stupid question, but I've only tried to use vectors once before :P
It depends on how your using the vector. I would suggest using it as: std::vector<Zombie> zombies, but you may be using it as: std::vector<Zombie*> zombies. If you are using the latter, it is still prone to memory errors, and you will have to manually call delete on elements before erase()ing them. If you go with the former, the std::vector will handle everything for you.
Alright, I've finally gotten those darn vectors to work with me and not against me and... appearently it wasn't the zombie-pointers that caused my problem... I've tried to "convert" (basically deleting a '*' and replaced all -> with '.') for all pointers I had, but I _STILL_ have the same problem! So, I reverted back to using pointers (except for all the zombies, too much work to revert...) and now I've this weird thing again; if I play for long enough the errors disappear and it closes the application successfully.

This is really starting to get annoying now, since the error happens when I exit the main() function - after it returns 0, so I've got no clue in how to debug this either.

Anyways, here's the code (and if you want to whine about how some of my coding looks, go ahead - but give examples on how to improve it as well...):

http://pastebin.com/ZNXLpskQ

If anyone has got the time to look at it, I'd really appreciate it since I've got no clue at all on how to fix this.

Using SFML2 RC, if anyone actually takes a look at it :P

Thanks in advance :)
Topic archived. No new replies allowed.