Unhandled Exception, Access Violation

Apr 19, 2011 at 12:06pm
Hi,

I have been having this problem for a few days now and I can find nothing on how to fix it. I am not exactly new to c++ but I have never had an error like this one :
Unhandled exception at 0x014621fa in Tanks.exe: 0xC0000005: Access violation reading location 0xfeeefeee.


The program that I am writing currently has one class that will eventually be used to draw menus, using a library that my university has written themselves named Gwin.

As it stands I am not even sure where the error is coming from as when I use the debugger all the variables seem to be in order and there is nothing out of place that I can see. I have posted most of the code below:

Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "stdafx.h"

int main()
{
	GWindow Gwin;
	
	Gwin.clear();
	Gwin.setPenColour(WHITE);

	GImage Title("Start.png");
	GImage  Background("Background.jpg");

	Menu Start("Start", Title, 50, 0, Background, Gwin);
	Start.AddOption(Title, 1);
	Start.DrawMenu(Gwin);
	
	Keyboard.waitKey();

	return 0;
}

stdafx.h
1
2
3
4
5
6
#pragma once

#include <iostream>

#include "gwin.h"
#include "GLOBALS.h" 


GLOBALS.h
1
2
3
4
5
6
7
8
#include "stdafx.h"
#include "gwin.h"
#include <string.h>
#include <vector>

using namespace std;

#include "Menu.h" 


Menu.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#pragma once

class Menu
{
public:
	//Functions
	Menu(string Title, GImage Picture, int x, int y, GImage Bkg, GWindow &Gwin);
	~Menu(void);
	void DrawMenu(GWindow &Gwin);
	void EventManagement(void);
	void AddOption(GImage Opt, int Code);

	//Variables
private:
	string MenuTitle;
	GImage TitlePicture;
	int Tposx, Tposy;
	GImage Background;
	vector<GImage> Options;
	vector<int> OptionCodes;
	int ScreenSizeH;
	int ScreenSizeW;
};


Menu.cpp
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
#include "StdAfx.h"

Menu::Menu(string Title, GImage Picture, int x, int y, GImage Bkg, GWindow &Gwin)
{
	MenuTitle = Title;
	TitlePicture = Picture;
	Tposx = x;
	Tposy = y;
	Background = Bkg;

	ScreenSizeH = Gwin.getHeight();
	ScreenSizeW = Gwin.getWidth();
}

Menu::~Menu(void)
{
	
}

void Menu::DrawMenu(GWindow &Gwin)
{
	Gwin.drawImage(0, 0, &Background); //Draw the background
	Gwin.drawImage(Tposx, Tposy, &TitlePicture); //Draw the title picture

	if(OptionCodes.size() == 0)
	{
	}

	else
	{
		if(OptionCodes.size() != Options.size()) //Check to make sure that there are the same amout of options as optioon codes
		{
			return;
		}

		for(int i = 0; i < OptionCodes.size(); i++) //Iterate through the codes
		{
			Gwin.drawImage(ScreenSizeW, (ScreenSizeH-(i*10)), &Options[i]); //draw the images for the options
		}
	}

}

void Menu::EventManagement(void)
{
}

void Menu::AddOption(GImage Opt, int Code)
{
	Options.push_back(Opt);
	OptionCodes.push_back(Code);
}


Sorry about there being so much code, but I really am at a loss of what to do! Any help would be greatly appreciated! Thanks.
Apr 19, 2011 at 1:26pm
0xfeeefeee is an address that appears in DEBUG builds (under Visual Studio anyway) when a pointer has not been set to anything. (it's a helper to find memory leaks)

You'll have to step through your code or set a break point near where the error occurs or set break points around any pointers in the code and look for a pointer that contains 0xfeeefeee at the point of the exception. From there you can workout how to properly initialise and delete the pointer correctly.
Apr 19, 2011 at 1:39pm
You should determine the line where it happens
Apr 19, 2011 at 1:44pm
closed account (z05DSL3A)
0xfeeefeee is used to mark freed heap memory.
Apr 19, 2011 at 4:21pm
Thanks Guys. However I am not sure how the memory is being freed, and I am not using any pointers, only references...

When I set breakpoints and run through the code it appears to have an issue with the call to:
Start.DrawMenu(Gwin);
in main.cpp

Any more help would be greatly appreciated!
Apr 19, 2011 at 4:36pm
Set a breakpoint at that line and have a look at the Watch window (If you use visual studio it's under the Debug menu, Windows->Watch->Watch1.
Type Start into the window that pops up and also Gwin and look for any pointers containing 0xfeeefeee. That should give us a clue on where to look next.
Apr 19, 2011 at 6:56pm
Have done as you said, however before that point all the variables check out. After that point all the watch window says is that it cannot find the sybmbols for example:
Start CXX0017: Error: symbol "Start" not found


I am really not sure why this is, other than that maybe something corrupts all the memory that my program is using?

Thanks.
Apr 19, 2011 at 7:04pm
Some more information: After looking at it more closely the error seems to be happening when I call Start.AddOption() within the main.cpp file.

Thanks.
Apr 19, 2011 at 7:04pm
OK, so your best bet is probably going to be stepping through the code (using F10 and F11 I think in Visual Studio) and seeing where/when they dissapear, using F11 you can step into the functions where Start and GWin disappear and maybe see whats going on?

Seems a bit wierd that you can't see Start when a breakpoint is set on that line though?!??

where does "gwin.h" and it's library/source come from?
Apr 19, 2011 at 7:43pm
OK, have gone through the code step by step, but something has confused me even more. Basically, it is completely fine, then at the end of the function AddOptions(), where the curly ending bracket is, it throws the error, not during the statements to add stuff into the vectors... So yes, I really don't understand that. Any ideas?

Thanks Again.
Apr 19, 2011 at 7:54pm
Like you say, there's something not right with how Options (or how a new GImage) is made.

eg
GImage Title("Start.png");
makes a new GImage using a bitmap,
but does
GImage Title;
make a valid, initialised instance?

I think I'd have to try it myself to see if I get the same here, but I'd need the gwin.h and it's .cpp or .lib file.

Is there a website for it or is it also your code?
Last edited on Apr 19, 2011 at 7:59pm
Apr 19, 2011 at 8:33pm
If it happens at the end of a function, it could be a problem with destructors. Are you passing pointers to stack variables anywhere? The functions/classes might be trying to delete them or possibly use them after they are destroyed.
Apr 19, 2011 at 11:09pm
@ Moooce - The Gwin stuff is something that my university created. There is an msi installer for it, but it only works in visual studio as far as I am aware. You should be able to download it from here if you wanted to: https://warren.ntu.ac.uk/projects/gwin/files

@firedraco - thanks, will look into that as well!
Apr 20, 2011 at 12:15pm
OK, I have looked to see about passing pointers and things, but I am not doing that anywhere so I don't think it can be that. Also, I was wondering if I am using the private keyword correctly in my class declaration?

Thanks again!
Apr 20, 2011 at 12:27pm
Sorry for tripple posting but: I removed the call to AddOptions() and it works fine, however then, when I end the program it gives me the same error... So I don't think it something exactly to do with that function...
Apr 20, 2011 at 2:14pm
If you're using Visual Studio, turn on Exception trapping and run in the debugger. The program will stop when the exception is raised. Break into it (it'll prompt you) and take a look at the call stack.
Topic archived. No new replies allowed.