Dev-Cpp "Access violation"!? Why?

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
gamescoper
Posts: 7
Joined: Fri Feb 01, 2008 9:33 pm

Dev-Cpp "Access violation"!? Why?

Post by gamescoper »

I was debugging my code as it wasnt working and devcpp kept giving me this error "An Access Violation (Segmentation Fault) raised in your program." I know it has something to do with a pointer being corrupted or something but I am not greatly well versed in C++ so i am quite lost.

The debugger pointed to this line being the "violater":

Code: Select all

    io::IXMLReader* xml = device->getFileSystem()->createXMLReader("settings.xml"); 
I have the source code here:

Code: Select all

#include "include/irrlicht.h"
#include "include/irrxml.h"
#include <string>
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;

#ifdef _IRR_WINDOWS_
#pragma comment(lib, "Irrlicht.lib")
#endif
IrrlichtDevice *device = 0;
core::stringw width;
core::stringw height;
core::stringw name;
core::stringw slogan;
s32 cnt = 0;
IGUIListBox* listbox = 0;
class MyEventReceiver : public IEventReceiver
{
public:
	virtual bool OnEvent(const SEvent& event)
	{
		if (event.EventType == EET_GUI_EVENT)
		{
			s32 id = event.GUIEvent.Caller->getID();
			IGUIEnvironment* guienv = device->getGUIEnvironment();
			switch(event.GUIEvent.EventType)
{
    case EGET_BUTTON_CLICKED:

				if (id == 1)
				{
					guienv->clear();
					guienv->addButton(rect<s32>(88, 64, 304, 96), 0, 5, L"Back");
                    guienv->addStaticText(L"Story mode in develpoment", rect<s32>(88, 16, 304, 36), false, false, 0, -1);
					return true;
				}
                if (id == 2)
                {
                       guienv->clear();
                       guienv->addButton(rect<s32>(88, 64, 304, 96), 0, 5, L"Back");
                       guienv->addStaticText(L"Multiplayer mode in develpoment", rect<s32>(88, 16, 304, 36), false, false, 0, 5);
                }
                if (id == 5)
                {
                       guienv->clear();
                       guienv->addButton(rect<s32>(88, 200, 304, 240), 0, 4, L"Exit");
	                   guienv->addButton(rect<s32>(88, 152, 304, 192), 0, 3, L"Options");
                       guienv->addButton(rect<s32>(88, 104, 304, 144), 0, 2, L"Multiplayer");
                       guienv->addButton(rect<s32>(88, 56, 304, 96), 0, 1, L"Story Mode");
                       guienv->addStaticText(L"Game menu", rect<s32>(88, 16, 304, 36), false, false, 0, -1);
                }
                if (id == 4)
                {
                       device->closeDevice();
                }
                if (id == 3)
                {
                       guienv->clear();
                       
                }                     


}
}
return false;}
};

int main()
{

//XML settisdaadasdngs reader
	// read configuration from xml file
    io::IXMLReader* xml = device->getFileSystem()->createXMLReader("settings.xml");
	while(xml && xml->read())
	{
		switch(xml->getNodeType())
		{
		case io::EXN_TEXT:
			// in this xml file, the only text which occurs is the messageText
			//MessageText = xml->getNodeData();
			break;
		case EXN_ELEMENT:
			{
				if (core::stringw("windowsize") == xml->getNodeName())
                   {
					width = xml->getAttributeValue(L"width");
					height = xml->getAttributeValue(L"width");
                   }
				else
				if (core::stringw("userdata") == xml->getNodeName())
				   	{
                    name = xml->getAttributeValue(L"name");
					slogan = xml->getAttributeValue(L"slogan");
                    }
			}
			break;
		}
	}
	if (xml)
		xml->drop(); // don't forget to delete the xml reader 


//End XML settings reader

    MyEventReceiver receiver;
    device = createDevice( video::EDT_SOFTWARE, dimension2d<s32>(640, 480), 32, true, false, false, &receiver);
    device->setWindowCaption(L"Window");
    IVideoDriver* driver = device->getVideoDriver();
	ISceneManager* smgr = device->getSceneManager();
	IGUIEnvironment* guienv = device->getGUIEnvironment();
	guienv->addButton(rect<s32>(88, 200, 304, 240), 0, 4, L"Exit");
	guienv->addButton(rect<s32>(88, 152, 304, 192), 0, 3, L"Options");
    guienv->addButton(rect<s32>(88, 104, 304, 144), 0, 2, L"Multiplayer");
    guienv->addButton(rect<s32>(88, 56, 304, 96), 0, 1, L"Story Mode");
    guienv->addStaticText(L"WW2D", rect<s32>(88, 10, 304, 36), false, false, 0, -1);
    guienv->addStaticText(L"Game menu", rect<s32>(88, 16, 304, 36), false, false, 0, -1);
/*IGUIImage *img501 = guienv->addImage(rect<s32>(168, 40, 268, 65), 0, -1, L"");
img501->setImage(guienv->getVideoDriver()->getTexture("C:/Documents and Settings/Sameh/My Documents/My Pictures/button.png"));*/



    while(device->run())
	{
		driver->beginScene(true, true, SColor(255,100,101,140));
		smgr->drawAll();
		guienv->drawAll();
		driver->endScene();
    }
device->drop();

}
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Re: Dev-Cpp "Access violation"!? Why?

Post by rogerborg »

gamescoper wrote:

Code: Select all

IrrlichtDevice *device = 0;
/...
    io::IXMLReader* xml = device->getFileSystem()->createXMLReader("settings.xml");
/...
    device = createDevice( video::EDT_SOFTWARE, dimension2d<s32>(640, 480), 32, true, false, false, &receiver);
}
You're using 'device' before you've set it to anything valid. You'll want to do createDevice() as soon as possible, so that device is valid.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
Yustme
Posts: 107
Joined: Sat Dec 01, 2007 10:50 pm

Re: Dev-Cpp "Access violation"!? Why?

Post by Yustme »

Hi,

You are using "device" right at the beginning (somewhere) in your main() function.

It isn't initialized. You initialize it under the while(xml && xml->read()).

You can fix it by putting (at least) the device initialization right above the xml code block like this:

Code: Select all


int main()
{
MyEventReceiver receiver;
    device = createDevice( video::EDT_SOFTWARE, dimension2d<s32>(640, 480), 32, true, false, false, &receiver);

//XML settisdaadasdngs reader
	// read configuration from xml file
    io::IXMLReader* xml = device->getFileSystem()->createXMLReader("settings.xml");
	while(xml && xml->read())
	{
		switch(xml->getNodeType())
		{
		case io::EXN_TEXT:
			// in this xml file, the only text which occurs is the messageText
			//MessageText = xml->getNodeData();
			break;
		case EXN_ELEMENT:
			{
				if (core::stringw("windowsize") == xml->getNodeName())
                   {
					width = xml->getAttributeValue(L"width");
					height = xml->getAttributeValue(L"width");
                   }
				else
				if (core::stringw("userdata") == xml->getNodeName())
				   	{
                    name = xml->getAttributeValue(L"name");
					slogan = xml->getAttributeValue(L"slogan");
                    }
			}
			break;
		}
	}
	if (xml)
		xml->drop(); // don't forget to delete the xml reader 
}
If you didn't understood it, ask your questions :D
gamescoper
Posts: 7
Joined: Fri Feb 01, 2008 9:33 pm

Post by gamescoper »

thanks for your but i want the window size to be affected by the settings in the xml file, if the device is initialised before the xml file is read can i re-initialise the device
randomMesh
Posts: 1186
Joined: Fri Dec 29, 2006 12:04 am

Post by randomMesh »

gamescoper wrote:thanks for your but i want the window size to be affected by the settings in the xml file, if the device is initialised before the xml file is read can i re-initialise the device
Use the NullDevice for reading, then drop() it and create another device.
gamescoper
Posts: 7
Joined: Fri Feb 01, 2008 9:33 pm

Post by gamescoper »

i edited this line so the second device is made with the variables from the xml file:

Code: Select all

    device = createDevice( video::EDT_SOFTWARE, dimension2d<s32>(width,height), 32, true, false, false, 0);
but now the compiler gives me these errors

Code: Select all

main.cpp: In function `int main()':
main.cpp:111: error: no matching function for call to `irr::core::dimension2d<irr::s32>::dimension2d(irr::core::stringw&, irr::core::stringw&)'
include/dimension2d.h:18: note: candidates are: irr::core::dimension2d<irr::s32>::dimension2d(const irr::core::dimension2d<irr::s32>&)
include/dimension2d.h:24: note:                 irr::core::dimension2d<T>::dimension2d(const T&, const T&) [with T = irr::s32]
include/dimension2d.h:21: note:                 irr::core::dimension2d<T>::dimension2d() [with T = irr::s32]

make.exe: *** [main.o] Error 1

Execution terminated
randomMesh
Posts: 1186
Joined: Fri Dec 29, 2006 12:04 am

Post by randomMesh »

gamescoper wrote:error: no matching function for call to `irr::core::dimension2d<irr::s32>::dimension2d(irr::core::stringw&, irr::core::stringw&)'
width and height have to be int, not stringw. ;)
gamescoper
Posts: 7
Joined: Fri Feb 01, 2008 9:33 pm

Post by gamescoper »

ahh thanks

but the variables have to be set as a stringw as the xml reader doesnt seem to output as integers
Dark_Kilauea
Posts: 368
Joined: Tue Aug 21, 2007 1:43 am
Location: The Middle of Nowhere

Post by Dark_Kilauea »

Perhaps you should read the XML reader documentation?

getAttributeValueAsInt() is probably what you're looking for.
rogerborg wrote:Every time someone learns to use a debugger, an angel gets their wings.
gamescoper
Posts: 7
Joined: Fri Feb 01, 2008 9:33 pm

Post by gamescoper »

i would look through the docs except i find it hard to englishly understand it and i dont know where to look for what since it just shows me the class name for c++ but doesnt categorise them like "XML Reader" or "3D Rendering" etc.

I personally think that putting them in nice categories would sharply reduce the questions in this forum but i guess its nico's choice or whoever runs this forum/documentation

Oh btw thanks
wyrmmage
Posts: 204
Joined: Sun Mar 16, 2008 3:12 am
Contact:

Post by wyrmmage »

Just so you know, this doesn't have anything to do with Dev-Cpp, it's just a problem with the code ;)
-wyrmmage
Worlds at War (Current Project) - http://www.awkward-games.com
Ganadu'r, The Eternal Sage (Other Current Project) - http://rpg.naget.com
Post Reply