addToDeletionQueue and drop() causing my application to crash

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
phillip
Posts: 14
Joined: Thu Nov 05, 2009 9:04 pm

addToDeletionQueue and drop() causing my application to crash

Post by phillip »

Hi everyone

When using addToDeletionQueue(cube) or cube->drop() This is causing my application to crash in windows. I'm simply adding a cube node and removing it from the scene manager. This is strange because I'm and not referencing it anywhere in my code. What I'm I doing wrong. Thank you in advance.


// Create a cube scene node and add it to the scene manager
ISceneNode* cube = smgr->addCubeSceneNode(20);

//then removed it without using it
//first caused crash
smgr->addToDeletionQueue(cube);

//second test caused crash
cube->drop();
AReichl
Posts: 269
Joined: Wed Jul 13, 2011 2:34 pm

Re: addToDeletionQueue and drop() causing my application to crash

Post by AReichl »

from ChatGPT:

The `addToDeletionQueue` function in the Irrlicht Engine is used to safely remove objects from the scene during the rendering process. If you need to remove something from inside `drawAll()` (in `OnPreRender`, `OnAnimate`, etc), then you should use `addToDeletionQueue`. This function prevents messing up the parent's Children's list iterator while Irrlicht is iterating through the list¹. It's a safer alternative to directly calling `remove` during these times¹. However, outside of these specific cases, you can safely call `remove` directly¹.

Quelle: Unterhaltung mit Bing, 30.4.2024
(1) Should I use remove() or SceneManager->addToDeletionQueue - Irrlicht Engine. viewtopic.php?t=32788.
(2) Irrlicht: how to enable shadows of all nodes in scene manager?. https://stackoverflow.com/questions/520 ... ne-manager.
(3) Skylicht Engine - GitHub. https://github.com/skylicht-lab/skylicht-engine.
CuteAlien
Admin
Posts: 9654
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: addToDeletionQueue and drop() causing my application to crash

Post by CuteAlien »

I don't think that's the full code. First one shouldn't crash and doesn't in tests here:

Code: Select all

#include <irrlicht.h>

using namespace irr;

#ifdef _MSC_VER
#pragma comment(lib, "Irrlicht.lib")
#endif

int main(int argc, char *argv[])
{
	IrrlichtDevice * Device = createDevice(video::EDT_OPENGL, core::dimension2d<u32>(640,480) );
	if (!Device)
		return false;
	
	scene::ISceneManager* smgr = Device->getSceneManager();
	video::IVideoDriver* videoDriver =  Device->getVideoDriver();	

	scene::ISceneNode* cube1 = smgr->addCubeSceneNode(20);
	smgr->addToDeletionQueue(cube1);

	while ( Device->run() )
	{
		if ( Device->isWindowActive() )
		{
			scene::ISceneNode* cube2 = smgr->addCubeSceneNode(20);
			smgr->addToDeletionQueue(cube2);

			scene::ISceneNode* cube3 = smgr->addCubeSceneNode(20);

			videoDriver->beginScene(true, true);

			smgr->drawAll();

			videoDriver->endScene();

			smgr->addToDeletionQueue(cube3);
		}
		Device->sleep( 1 );
	}

	scene::ISceneNode* cube4 = smgr->addCubeSceneNode(20);
	smgr->addToDeletionQueue(cube4);
	
	Device->closeDevice();
	Device->drop();
	
	return 0;
}
The drop on the other hand will crash later because you should never call drop unless
a) you called grab()
b) you used an Irrlicht function starting with thew word create
c) you create the object with new
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
phillip
Posts: 14
Joined: Thu Nov 05, 2009 9:04 pm

Re: addToDeletionQueue and drop() causing my application to crash

Post by phillip »

Thank you for the response. @CuteAlien does the below lines in your while loop example code crash your application?

scene::ISceneNode* cube2 = smgr->addCubeSceneNode(20);
smgr->addToDeletionQueue(cube2);
CuteAlien
Admin
Posts: 9654
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: addToDeletionQueue and drop() causing my application to crash

Post by CuteAlien »

No, my example doesn't crash. Tested with 1.8 and trunk. If that code crashes then I'd guess something in your setup isn't correct. Like maybe it uses the wrong header/library combination (in case you work with more than one version of Irrlicht).
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
phillip
Posts: 14
Joined: Thu Nov 05, 2009 9:04 pm

Re: addToDeletionQueue and drop() causing my application to crash

Post by phillip »

Thank you @CuteAlien I think I found why my app is crashing. According your above example if cube1 is being created outside the loop so I can't call the delete the node at end of the end scene. it should be within the loop. I was doing the below code.

videoDriver->endScene();
smgr->addToDeletionQueue(cube1);

Code: Select all

int main(int argc, char *argv[])
{
	IrrlichtDevice * Device = createDevice(video::EDT_OPENGL, core::dimension2d<u32>(640,480) );
	if (!Device)
		return false;
	
	scene::ISceneManager* smgr = Device->getSceneManager();
	video::IVideoDriver* videoDriver =  Device->getVideoDriver();	

	scene::ISceneNode* cube1 = smgr->addCubeSceneNode(20);
	
	while ( Device->run() )
	{
		if ( Device->isWindowActive() )
		{
			videoDriver->beginScene(true, true);

			smgr->drawAll();

			videoDriver->endScene();

			smgr->addToDeletionQueue(cube1);
		}
		Device->sleep( 1 );
	}

	Device->closeDevice();
	Device->drop();
	
	return 0;
}


CuteAlien
Admin
Posts: 9654
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: addToDeletionQueue and drop() causing my application to crash

Post by CuteAlien »

OK, problem in that case is that you add it to the deletion qeue over and over every loop. In theory - in reality it'll crash second time you do that. It doesn't check if what you try to delete still can be deleted, but trusts you there. You could for example set cube1 to 0 after adding it and only add it when it's not 0. Or any other way - just ensure you don't try to delete it twice.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Post Reply