Access violation reading location

So Im using SDL to make a zombie survival game and Im getting a weird error
It has to do with 2 files one where zombies spawn and one where they move.
It is saying that i am reading from a bad location. Heres the code

ZombSpawn
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
int zombSpawnF(void *data)
{
	bool isWaveDone = false;
	int* wavenum = (int*)data;
	if(*wavenum == 0)
	{
		short g = 0;
		zombie *zSpawn =NULL;
		zSpawn = new zombie[10];
		while(g!=9)
		{
			zSpawn[g].zombie::zombie(diffucluty);
			g++;
		}
		SDL_Thread *zombieMove = SDL_CreateThread(zombieMoved,&zSpawn[0]);
		SDL_Thread *zombieMove2 = SDL_CreateThread(zombieMoved,&zSpawn[1]);
		SDL_Thread *zombieMove3 = SDL_CreateThread(zombieMoved,&zSpawn[2]);
		SDL_Thread *zombieMove4 = SDL_CreateThread(zombieMoved,&zSpawn[3]);
		SDL_Thread *zombieMove5 = SDL_CreateThread(zombieMoved,&zSpawn[4]);
		SDL_Thread *zombieMove6 = SDL_CreateThread(zombieMoved,&zSpawn[5]);
		SDL_Thread *zombieMove7 = SDL_CreateThread(zombieMoved,&zSpawn[6]);
		SDL_Thread *zombieMove8 = SDL_CreateThread(zombieMoved,&zSpawn[7]);
		SDL_Thread *zombieMove9 = SDL_CreateThread(zombieMoved,&zSpawn[8]);
		SDL_Thread *zombieMove10 = SDL_CreateThread(zombieMoved,&zSpawn[9]);
	}
	else if(*wavenum == 1)
	{
		short g = 0;
		zombie *zSpawn =NULL;
		zSpawn = new zombie[20];
		while(g!=9)
		{
			zSpawn[g].zombie::zombie(diffucluty);
			g++;
		}
		SDL_Thread *zombieMove = SDL_CreateThread(zombieMoved,&zSpawn[0]);
		SDL_Thread *zombieMove2 = SDL_CreateThread(zombieMoved,&zSpawn[1]);
		SDL_Thread *zombieMove3 = SDL_CreateThread(zombieMoved,&zSpawn[2]);
		SDL_Thread *zombieMove4 = SDL_CreateThread(zombieMoved,&zSpawn[3]);
		SDL_Thread *zombieMove5 = SDL_CreateThread(zombieMoved,&zSpawn[4]);
		SDL_Thread *zombieMove6 = SDL_CreateThread(zombieMoved,&zSpawn[5]);
		SDL_Thread *zombieMove7 = SDL_CreateThread(zombieMoved,&zSpawn[6]);
		SDL_Thread *zombieMove8 = SDL_CreateThread(zombieMoved,&zSpawn[7]);
		SDL_Thread *zombieMove9 = SDL_CreateThread(zombieMoved,&zSpawn[8]);
		SDL_Thread *zombieMove10 = SDL_CreateThread(zombieMoved,&zSpawn[9]);
		SDL_Thread *zombieMove11 = SDL_CreateThread(zombieMoved,&zSpawn[10]);
		SDL_Thread *zombieMove12 = SDL_CreateThread(zombieMoved,&zSpawn[11]);
		SDL_Thread *zombieMove13 = SDL_CreateThread(zombieMoved,&zSpawn[12]);
		SDL_Thread *zombieMove14 = SDL_CreateThread(zombieMoved,&zSpawn[13]);
		SDL_Thread *zombieMove15 = SDL_CreateThread(zombieMoved,&zSpawn[14]);
		SDL_Thread *zombieMove16 = SDL_CreateThread(zombieMoved,&zSpawn[15]);
		SDL_Thread *zombieMove17 = SDL_CreateThread(zombieMoved,&zSpawn[16]);
		SDL_Thread *zombieMove18 = SDL_CreateThread(zombieMoved,&zSpawn[17]);
		SDL_Thread *zombieMove19 = SDL_CreateThread(zombieMoved,&zSpawn[18]);
		SDL_Thread *zombieMove20 = SDL_CreateThread(zombieMoved,&zSpawn[19]);
	}


ZombMove
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
int zombieMoved(void *data)
{
	zombie* gen = (zombie*)data;
	while(gen ->isZombAlive)
	{
		if(p.x < gen->x)
		{
			gen -> move_Left();
			if(p.x == gen ->x || p.x == gen->farX||p.farX == gen ->x|| p.farX == gen->farX)
			{
				if(p.y == gen->y||p.y == gen->farY||p.farY==gen->y||p.farY==gen->farY)
				{
					p.~player();
					return 0;
				}
			}
			if(bx == gen->x||bx == gen->farX)
			{
				if(by == gen ->farY||by==gen->y)
				{
					delete gen; 					return 0;
				}
			}
		}
		else if(p.x > gen ->x)
		{
			gen -> move_Right();
			if(p.x == gen ->x || p.x == gen->farX||p.farX == gen ->x|| p.farX == gen->farX)
			{
				if(p.y == gen->y||p.y == gen->farY||p.farY==gen->y||p.farY==gen->farY)
				{
					p.~player();
					return 0;
				}
			}
			if(bx == gen->x||bx == gen->farX)
			{
				if(by == gen ->farY||by==gen->y)
				{
					delete gen;					return 0;
				}
			}
		}
		if(p.y < gen ->y)
		{
			gen->move_Up();
		/*error said to be here*/if(px == gen ->x || px == gen->farX||pfx == gen ->x|| pfx == gen->farX)
			{
		/*error said to be here*/if(py == gen->y||py == gen->farY||pfy==gen->y||pfy==gen->farY)
				{
					p.~player();
					return 0;
				}
			}
			if(bx == gen->x||bx == gen->farX)
			{
				if(by == gen ->farY||by==gen->y)
				{
					delete gen;					return 0;
				}
			}
		}
		else if(p.y > gen->y)
		{
			gen -> move_Down();
		/*error said to be here*/ if(px == gen ->x || px == gen->farX||pfx == gen ->x|| pfx == gen->farX)
			{
		/*error said to be here*/if(py == gen->y||py == gen->farY||pfy==gen->y||pfy==gen->farY)
				{
					p.~player();
					return 0;
				}
			}
			/*error said to be here*/if(bx == gen->x||bx == gen->farX)
			{
				if(by == gen ->farY||by==gen->y)
				{
					delete gen;					return 0;
				}
			}
		}
	}
	return 0;
}

Sorry For making it so lengthy
My hypothesis is that for some reason zombie gen doesnt exist. I have no idea why. Help
Last edited on
closed account (zb0S216C)
An access violation is commonly caused by accessing an invalid pointer. You can simply resolve this (and future access violations) by check for a null pointer before using it, preferably, at the top of a function. For example:

1
2
3
4
5
6
7
void Function( int *&Pointer )
{
    if( Pointer == nullptr )
        return;

    // Continue.
}

Also,

matorin57 wrote:
1
2
3
return 0;
SDL_Thread *zombieMove = SDL_CreateThread(zombieMoved,&zSpawn[0]);
// ... 
(sic)

Once a return statement is encountered, the function terminates and any trailing statements are not executed.

Wazzak
Last edited on
Thx for the help
Added that and it still says the same error? Ok now im Completely lost.
What IDE (Visual Studio, Eclipse, Code::Blocks etc) and what toolchain (gcc, VC++ etc) are you using? This sounds like a good reason to learn how to properly use a debugger to me.
Im using Visual Studio 2010 and VC++
Last edited on
You're aware that threads run simultaneously? When data structures can be both accessed and modified across several threads, you need to synchronize access to them (using critical sections/mutexes). Your code currently doesn't look very thread-safe.

Also,
gen ->~zombie();
You never* call the destructor yourself. When you want to delete an object that was created with new, use delete. Automatic objects are destroyed automatically when they go out of scope.
Last edited on
Oh and while we're at it, you don't wanna create a billion threads for every single zombie.
Athar can you point out some insecurites in my thread code that i could fix to make it more secure?
And Thanks for the Article hanst99
It seems that you're using p in multiple threads. It's also possible that several threads attempt to destroy it at the same time. The move functions might also use shared structures, but that depends on how they're implemented.
Topic archived. No new replies allowed.