Skip to main content
GameDev.net gamedev.net
🔒 Locked

Can't find the access violation problem (NULL pointer?)

Started by GH33DA May 3, 2008 at 6:00 PM 8 replies 2.2k views
Original Post
GH33DA
GH33DA
Error: 0xC0000005: Access violation writing location 0x00000010. My Guess: A null pointer somewhere I missed. Can't find it though. Code: main.cpp
#ifdef WIN32
#pragma comment(lib, "SDL.lib")
#pragma comment(lib, "SDLmain.lib")
#endif

//#include <iostream>
//#include "Blok.h"
//#include "ColorMath.h"
//#include "Swatch.h"
#include "Engine.h"

//using namespace std;

int main(int argc, char *argv[])
{
	/*Blok Block1(1,1,BLACK,false);
	Blok Block2(2,2,RED,false);
	cout << Block1.get_color() << endl;
	cout << Block2.get_color() << endl;
	mix(Block1,Block2);
	cout << Block1.get_color() << endl;
	cout << Block2.get_color() << endl;
	cin.get();*/

	Engine theEngine;

	theEngine.SetTitle("SDL_Stuff");
	theEngine.Init();

	return 0;
}
engine.cpp
#include "Engine.h"

Engine::Engine()
{
	winWidth = 800;
	winHeight = 600;
	winTitle = 0;
	winScreen = 0;
}

Engine::~Engine()
{
	SDL_Quit();
}

void Engine::Input()
{
}

void Engine::Render()
{
}

void Engine::Logic()
{
}

void Engine::SetSize(const int& width, const int& height)
{
	winWidth = width;
	winHeight = height;
	winScreen = SDL_SetVideoMode( width, height, 0, SDL_ANYFORMAT);
}

SDL_Surface* Engine::GetScreen()
{
	return winScreen;
}

void Engine::Init()
{

	if( SDL_Init(SDL_INIT_VIDEO) < 0)
	{
		fprintf(stderr,"SDL failed to initalize.");
		SDL_Quit();
	}

	SetSize( winWidth, winHeight);

	if( winScreen == NULL)
	{
		fprintf(stderr,"SDL failed to display screen.");
		SDL_Quit();
	}
}

void Engine::Loop()
{
}

void Engine::SetTitle(const char* title)
{
	winTitle = title;
	SDL_WM_SetCaption( title, 0);
}
Stowelly
Stowelly
which line does it break on?

either use a debugger, or brute force output text before and after lines to try and assess where it is breaking
http://stowelly.co.uk/
Simian Man
Simian Man
The code you posted looks fine at a glance, but why haven't you used a debugger??
Stowelly
Stowelly
Actually i think i see it now

you dont initialise SDL before calling setTitle (SDL_WM_SetCaption( title, 0);)
http://stowelly.co.uk/
GH33DA
GH33DA
Quote:
Original post by Stowelly
which line does it break on?

either use a debugger, or brute force output text before and after lines to try and assess where it is breaking


I've put breakpoint on all the lines, it gives me the violation the line after "return 0" in main.cpp.
LessBread
LessBread
Quote:
Original post by GH33DA
Error:
0xC0000005: Access violation writing location 0x00000010.


0x00000010

This address indicates that you're trying to access an item 16 bytes passed the null pointer - likely a member of an object or a structure that was not properly instanced (e.g. allocated using new or malloc).
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
Palidine
Palidine
Try taking SDL_Quit() out of your Engine destructor and call it right before return 0; instead.

-me
GH33DA
GH33DA
Quote:
Original post by Stowelly
Actually i think i see it now you dont initialise SDL before calling setTitle (SDL_WM_SetCaption( title, 0);)


Same results

Quote:
Original post by Palidine
Try taking SDL_Quit() out of your Engine destructor and call it right before return 0; instead.

-me


Same results


here's my Engine.h:
#pragma once#include "SDL.h"class Engine{private:	int winWidth;	int winHeight;	const char* winTitle;	SDL_Surface* winScreen;protected:	void Input();	void Render();	void Logic();	void SetSize(const int& width, const int& height);public:	Engine();	virtual ~Engine();	SDL_Surface* GetScreen();	void Init();	void Loop();		void SetTitle(const char* title);};
Stowelly
Stowelly
Quote:
Original post by GH33DA
Quote:
Original post by Stowelly
Actually i think i see it now you dont initialise SDL before calling setTitle (SDL_WM_SetCaption( title, 0);)


Same results


[/quote]

what did you change?, did you just comment out the line?, where did you set the breakpoints? from the sounds of it you just put them on the lines in your main function.... step through the initialisation line by line and check everything is valid including return values etc
http://stowelly.co.uk/
GH33DA
GH33DA
Quote:
Original post by Stowelly

what did you change?, did you just comment out the line?, where did you set the breakpoints? from the sounds of it you just put them on the lines in your main function.... step through the initialisation line by line and check everything is valid including return values etc


I swapped the Init and SetTitle lines.
I'll step through the functions.

EDIT:

I stepped through the functions. I seems to happen right after the SQL_Quit(); in the deconstructor in the Engine.cpp.

EDIT AGAIN:

I told the linker to ignore msvcrt.lib. That seems to have fixed the problem.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.