Newb C++ issue

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
darksmaster923
Posts: 51
Joined: Tue Jan 02, 2007 11:04 pm
Location: huntington beach

Newb C++ issue

Post by darksmaster923 »

Alright, so I finally started to actually start learning C++ and irrlicht seriously. I'm tinkering around with irrlicht, and I'm coming up with what looks like to be a C++ code error, where it says "Unhandled exception at 0x00be1c24 in irrlicht.exe: 0xC0000005: Access violation reading location 0xcccccccc." when I exit the program, and graphics says

Code: Select all

irr::IReferenceCounted	CXX0030: Error: expression cannot be evaluated	

Heres my code

init.h

Code: Select all

#include "irrlicht.h"
#include "driverChoice.h"
#include "iostream"



class initGame
{

public:

	initGame();

	int CreateDevice();
	void StartGame();

	irr::IrrlichtDevice *graphics;
	float time;

};
init.cpp

Code: Select all

#include "StdAfx.h"
#include "init.h"

using namespace irr;
using namespace std;

#ifdef _IRR_WINDOWS_
#pragma comment(lib, "Irrlicht.lib")
#pragma comment(linker, "/subsystem:windows /ENTRY:mainCRTStartup")
#endif





int main()
{

	initGame Init;
	initGame();
	Init.CreateDevice();
	Init.StartGame();

	return 0;
}

initGame::initGame()
{

}

void initGame::StartGame()
{
	graphics->setWindowCaption(L"SAFSSDFSDFS");
}

int initGame::CreateDevice()
{

	video::E_DRIVER_TYPE driverType;

	IrrlichtDevice *graphics = createDevice(video::EDT_DIRECT3D9, core::dimension2d<u32>(1024,768),32,false,true,false,0);
	/*
	if(!graphics)
	{
		return 1;	//returns 1
	}
	*/

	//KeyListener receiver;

	graphics->setWindowCaption(L"Test");
	video::IVideoDriver *driver=graphics->getVideoDriver();
	scene::ISceneManager *smgr=graphics->getSceneManager();
	gui::IGUIEnvironment* gui=graphics->getGUIEnvironment();
	gui::IGUISkin* skin = gui->getSkin();
	gui::IGUIFont* font = gui->getFont("../../media/fonthaettenschweiler.bmp");


	while(graphics->run())
	{

		//core::vector3df playerposition=playercam->getAbsolutePosition();


		driver->beginScene(true, true, video::SColor(255,20,20,100));
		smgr->drawAll();
		gui->drawAll();
		driver->endScene();
	}

	graphics->drop();


	return 0;
}

thanks in advance
Programmers are merely tools to convert caffeine into code.
ChaiRuiPeng
Posts: 363
Joined: Thu Dec 16, 2010 8:50 pm
Location: Somewhere in the clouds.. drinking pink lemonade and sunshine..

Post by ChaiRuiPeng »

i put in comments. one of your biggest errors was calling the constructor explicitly.
you dont do that. an objects constructor gets called auto when it is created.

Code: Select all

#include <irrlicht.h>
#include "driverChoice.h"
#include "iostream"



class initGame
{

public:


   initGame();

   int CreateDevice();
   void StartGame();

   irr::IrrlichtDevice* graphics;
   float time;


};

Code: Select all

#include "StdAfx.h"
#include "init.h"

using namespace irr;
using namespace std;

#ifdef _IRR_WINDOWS_
#pragma comment(lib, "Irrlicht.lib")
#pragma comment(linker, "/subsystem:windows /ENTRY:mainCRTStartup")
#endif





int main()
{

   initGame Init;
   initGame(); //you do not explicitly call the constructor ;)
   Init.CreateDevice();
   Init.StartGame();

   return 0;
}

initGame::initGame():graphics(0), time(0.f) //this is an initializer list
{

}

void initGame::StartGame()
{
   graphics->setWindowCaption(L"SAFSSDFSDFS");
}

int initGame::CreateDevice()
{

   video::E_DRIVER_TYPE driverType; //what is this for?

    //you dont need to declare a second one
   graphics = createDevice(video::EDT_DIRECT3D9, core::dimension2d<u32>(1024,768),32,false,true,false,0);


   if(!graphics)
   {                //you dont really need this comment :P
      return 1;   //returns 1
   }


   //KeyListener receiver;

   graphics->setWindowCaption(L"Test");
   video::IVideoDriver *driver=graphics->getVideoDriver();
   scene::ISceneManager *smgr=graphics->getSceneManager();
   gui::IGUIEnvironment* gui=graphics->getGUIEnvironment();
   gui::IGUISkin* skin = gui->getSkin();
   gui::IGUIFont* font = gui->getFont("../../media/fonthaettenschweiler.bmp");


   while(graphics->run())
   {

      //core::vector3df playerposition=playercam->getAbsolutePosition();


      driver->beginScene(true, true, video::SColor(255,20,20,100));
      smgr->drawAll();
      gui->drawAll();
      driver->endScene();
   }

   graphics->drop();


   return 0;
}
EDIT: also i dont see why you call createDevice() and THEN startGame()

seeing there is a while loop in createDevice() wouldnt it make mroe sense to createDevice(), and then have the while loop in startGame? :wink:
ent1ty wrote: success is a matter of concentration and desire
Butler Lampson wrote: all problems in Computer Science can be solved by another level of indirection
at a cost measure in computer resources ;)
hayate
Posts: 43
Joined: Mon Feb 16, 2009 9:38 pm
Location: Brescia, Italy

Re: Newb C++ issue

Post by hayate »

darksmaster923 wrote:

Code: Select all


int main()
{

	initGame Init;
	initGame();
	Init.CreateDevice(); <------------ PROBLEM HERE
	Init.StartGame(); <--------------- PROBLEM HERE

	return 0;
}

void initGame::StartGame()
{
	graphics->setWindowCaption(L"SAFSSDFSDFS");
}

int initGame::CreateDevice()
{

	video::E_DRIVER_TYPE driverType;

	IrrlichtDevice *graphics = createDevice(video::EDT_DIRECT3D9, core::dimension2d<u32>(1024,768),32,false,true,false,0);
	/*
	if(!graphics)
	{
		return 1;	//returns 1
	}
	*/

	while(graphics->run())
	{

	}

	graphics->drop();

	return 0;
}

You call graphics->setWindowCaption(L"SAFSSDFSDFS"); in StartGame() but the Irrlicht Device "graphics" has been deleted at the end of CreateDevice()
I also suggest to not put the rendering loop in CreateDevice() :wink:
Sorry for my awful english ^_^'
Image
darksmaster923
Posts: 51
Joined: Tue Jan 02, 2007 11:04 pm
Location: huntington beach

Post by darksmaster923 »

I thought that referencing it in the header file would let it 'survive' across multiple functions? Like a global variable?
Destructavator
Posts: 40
Joined: Sat Feb 06, 2010 9:00 pm
Location: Ohio, USA

Post by Destructavator »

If you haven't already, I'd strongly suggest taking one of the tutorial demos and trying to modify it, rather than starting from scratch.

If you really want to work with an example that is broken into multiple files, instead of having everything slammed into one main.cpp - which is what most of the tutorials do, and isn't how most real programs are laid out in C++ - I'd suggest going through the TechDemo example, one of the last ones in the tutorial list IIRC. It uses multiple files, has a config menu that comes up before the actual "game" starts, and in other ways is closer to a real game compared to, say, the first "Hello World" program.

If you can add to or change things in that Tech Demo and make the changes/additions work (start by changing just a few lines here and there to see the results), that might be an easier starting point compared to writing a lot of lines of code from scratch and not knowing which ones or how many lines are causing a problem.
- - - -
"Destructavator" Dave: Developer of OS GPL VST "ScorchCrafter" Audio Plug-ins, contributer to UFO AI, Day Job: Guitar Luthier (Customize musical instruments, repainting w/ custom artwork, graphics, logos, decals, etc.)
Radikalizm
Posts: 1215
Joined: Tue Jan 09, 2007 7:03 pm
Location: Leuven, Belgium

Post by Radikalizm »

You're calling graphics->drop() at the end of your createDevice() function (which is kind of weird if you think about it), this will bring the object's reference count down to 0 which will cause it to get deleted

So now you have an invalid object reference which you try to use in your startGame() function, causing unwanted and undefined behaviour


It might be a good idea to read up on some tutorials about pointers, they can be pretty intimidating when you're new to C++, but they are a crucial part of the language

Also, to avoid confusion, try to keep the name of a function linked to the actual working of a function, a function called createDevice() should not contain a drawing loop and should definitely not destroy the created device right before returning
Destructavator
Posts: 40
Joined: Sat Feb 06, 2010 9:00 pm
Location: Ohio, USA

Post by Destructavator »

darksmaster923 wrote:I thought that referencing it in the header file would let it 'survive' across multiple functions? Like a global variable?
No, in this case, the way you wrote it out, it does not "survive" quite like what I think you want.

Looking at:

Code: Select all

   IrrlichtDevice *graphics = createDevice(video::EDT_DIRECT3D9, core::dimension2d<u32>(1024,768),32,false,true,false,0);
   /*
   if(!graphics)
   {
      return 1;   //returns 1
   }
   */

   while(graphics->run())
   {

   }

   graphics->drop();
The whole "game" runs from the "while" until it gets terminated, interrupted, runs into an error, or for some reason the device no longer "runs" and is stopped. It then "drops" the device, which sorta kills it and ends your program.

Your main game loop goes between the brackets in that "while" statement, which is where you start a scene, draw stuff, end the scene, and let it repeat. Right now it is empty.

For a simple game you could just have another class with all your main game loop stuff, and call some function that draws and does everything you want to happen while the game runs, inside that "while" statement.

Example:

Code: Select all

while(graphics->run())
   {
         Game->Run();  // Most of your game goes here!
   }
Of course you'd have to write more files and code to define the class "Game", and of course don't forget to create an instance of it before the while statement comes up and you start calling that Game::Run() function.


EDIT: Radikalizm posted at the same time I did, and probably explained a few things better than I could.
Like I said, try mucking around with one of the already-written tutorial demos, and see if you can "read" the code and figure out why things are located where they are in what parts of the code.

EDIT (2): Whoops! I mis-read parts of the thread, and forgot already some of the stuff you had in the original post already. I'm an idiot! (Actually, part of the issue is I'm running on three hours of sleep, after an all-nighter the night right before.) Sorry, it looks like only some of what I posted applies. :oops:
- - - -
"Destructavator" Dave: Developer of OS GPL VST "ScorchCrafter" Audio Plug-ins, contributer to UFO AI, Day Job: Guitar Luthier (Customize musical instruments, repainting w/ custom artwork, graphics, logos, decals, etc.)
Post Reply