Grabbing the Irrlicht device??

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
Taymo
Posts: 55
Joined: Mon May 22, 2006 1:06 am
Location: Colorado Springs - CTU
Contact:

Grabbing the Irrlicht device??

Post by Taymo »

I've been getting a run-time error when I'm closing the program, and when I debug I notice that it's the device's reference counting. So to fix the problem I use device->grab() right after the createDevice call and it fixes the problem.

So the question is, have I temporarily fixed a more major problem, or is there a reason that there's no reference to grabbing the device in the Irrlicht tutorials? There should be as the function name is createDevice..
Ride the Spiral Development:
spiralride.blogspot.com
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

The createDevice() call gives you a pointer to an IReferenceCounted object that already has a reference count reserved for you. You should not need to grab() it. By doing so, all you are doing is avoiding the destruction of the device.

Chances are that you have some other code that is using the device after the call to drop(). You have to do something to prevent that from happening.

Travis
Taymo
Posts: 55
Joined: Mon May 22, 2006 1:06 am
Location: Colorado Springs - CTU
Contact:

Post by Taymo »

Umm I don't know.. here's my main loop, don't read it all, but at the end you can tell that the g_pDevice->drop() is called at the end of all execution. As I debug, it breaks at that g_pDevice->drop(), saying I have a zeroed out pointer of which I can't find in the locals or autos of VSC++'s debugger. The crazy thing is the address is NOT the Irrlicht device, the device's address is normal, the reference counter is 0, and the debug pointer is bad like usual. I should also say that I found no other instance of g_pDevice->drop() in the whole project.

So, could I be indirectly dropping the device as I shutdown my processes?

Code: Select all

int main(int *argc, char **argv)
{
	//Quaury user on rendering API
	//char device;
	E_DRIVER_TYPE driverType;
	//printf("Press \"o\" for OpenGL or \"d\" for DirectX.\n");
	//std::cin >> device;
	//if(device == 'o')
	//	driverType = EDT_OPENGL;
	//else if(device = 'd')
	//	driverType = EDT_DIRECT3D9;
	//else
	//	driverType = EDT_SOFTWARE;


	driverType = EDT_DIRECT3D9;
	//Initialize the Irrlicht device
    g_pDevice = createDevice(driverType,                        //Driver type
							 core::dimension2d<s32>(640,480),   //Window demensions
							 32,							    //bpp in windowed mode only
							 false,								//fullscreen
							 false,                             //stencil buffer
							 false,								//vsync
							 0);								//event receiver, procs will load these
	g_pDevice->grab();
						
	//Initialize video and scene managers, and grab logger
	g_pLogger = g_pDevice->getLogger();
	g_pVideo = g_pDevice->getVideoDriver();
    g_pSceneMgr = g_pDevice->getSceneManager();
	
    g_pDevice->setWindowCaption(L"Ride The Sprial - Sinister Incpetions");  //Set caption

	//Init utilities
	g_pUtils = new Utilities(g_pDevice, g_pLogger, g_pVideo, g_pSceneMgr, g_curProc);

	//Init controls
	g_pControls = new CControls;

	//Create process's and load into map
	CGameplayProcess *gameplay = new CGameplayProcess;
	m_procs[std::string("Gameplay")] = gameplay;
	CMainMenuProcess *mainmenu = new CMainMenuProcess;
	m_procs[std::string("MainMenu")] = mainmenu;

	//Set current process as the demo1 process
	g_curProc = "MainMenu";

    //Run main game loop!
	std::string oldproc = g_curProc;
	/*I want my update processes to calculate before 
	Irrlicht's. Also this will separate game logic 
	and data from Irrlicht's render ops and data.
	*/
    while((m_procs[g_curProc]->Update()) && (g_pDevice->run()) && g_pVideo)
    {
		if(g_pDevice->isWindowActive()){
			//If process changed, init new one and shutdown old one.
			if(oldproc != g_curProc){
				m_procs[oldproc]->Shutdown();  //Shutdown old
				m_procs[g_curProc]->Init();    //Init new
				oldproc = g_curProc;           //Set oldproc
			}
			m_procs[g_curProc]->Init();    //Init new

			m_procs[g_curProc]->Render(); //Render
			//Set window caption
			core::stringw buffer(L"Ride The Sprial - Sinister Inceptions FPS: ");
			buffer += g_pVideo->getFPS();
			buffer += "  Tri Count: ";
			buffer += g_pVideo->getPrimitiveCountDrawn();
			g_pDevice->setWindowCaption(buffer.c_str());

			//Let other windows processes use processor time
			//This keeps the CPU usage down for this app
			g_pDevice->yield();
		}
    }

	//destroy all procs
	std::map<std::string, IProcess *>::iterator proc_it;
	for(proc_it = m_procs.begin(); proc_it != m_procs.end(); proc_it++){
		delete proc_it->second;
		proc_it->second = 0;
	}
	m_procs.clear();

    //g_pDevice deletes itself upon the last drop() reference
	delete g_pUtils;
	delete g_pControls;
    g_pDevice->drop(); // <----!!!!  Fix  bad pointer problem 
    return 0;
}
Ride the Spiral Development:
spiralride.blogspot.com
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

That grab() should not be necessary. I can't see any problems in the code that you've posted (so it's likely in code that you haven't posted) or debug the problem from that vague description of the actual crash.

Do you fancy providing a compilable example with resources? Failing that, the standard debugging practices apply: set a data breakpoint on the suspect memory to catch when it changes, and/or start eliding parts of your code to narrow down the cause.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

Taymo wrote: I should also say that I found no other instance of g_pDevice->drop() in the whole project.
Does your Utilities class have a destructor? Does that drop() the device pointer?

If that isn't it, I don't see anything else that is obvious. If the g_pDevice pointer is null before calling drop() in main(), then I'd guess you are setting it to null yourself or you have data corruption issues. Either way, the debugger can tell you.

Travis
Post Reply