[Solved] IFileSystem::createAndOpenFile returning null po...

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
fabs
Posts: 18
Joined: Sun Jun 25, 2006 1:41 pm

[Solved] IFileSystem::createAndOpenFile returning null po...

Post by fabs »

Hello all

I have a routine in a game I'm writing which loads and executes a script file. It uses the Irrlicht filesystem to support transparently loading the script from an archive or a directory.

The code works fine, except when the code is called a second (or further) time with the same arguments: the call to createAndOpenFile returns a null pointer. To solve this problem I am considering writing a script caching system, which will be beneficial anyway, but I am interested to know if I am misusing the Irrlicht filesystem or if there is another cause for this unusual problem.

Code: Select all

void CGame::launchScript(IrrlichtDevice *device, lua_State *L, std::string filename)
{
	std::cout << "Loading script " << filename << std::endl;
	io::IReadFile *scriptfile = device->getFileSystem()->createAndOpenFile(filename.c_str());
	char *scriptdata = new char[scriptfile->getSize() + 1];
	scriptfile->read(scriptdata, scriptfile->getSize());
	scriptdata[scriptfile->getSize()] = 0;
	scriptfile->drop();
	luaL_dostring(L, scriptdata);
	delete [] scriptdata;
}
Thanks for any input.
Last edited by fabs on Fri Jan 14, 2011 10:23 am, edited 1 time in total.
fabs
Posts: 18
Joined: Sun Jun 25, 2006 1:41 pm

Post by fabs »

I should add that the file is a physical file, not in an archive. I was therefore worried that the first opening of the file isn't getting closed, but there doesn't appear to be any method other than drop which would close the file.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

I never experienced such a problem, but your code is correct. drop will close the file and must be called to avoid mem leaks and ressource problems.
bitplane
Admin
Posts: 3204
Joined: Mon Mar 28, 2005 3:45 am
Location: England
Contact:

Post by bitplane »

Did your working directory change between calling this function?
Submit bugs/patches to the tracker!
Need help right now? Visit the chat room
fabs
Posts: 18
Joined: Sun Jun 25, 2006 1:41 pm

Post by fabs »

Well this is a bit embarassing... there was indeed a call to change the working directory in a superclass method /after/ it calls the script, so it works the first time and fails on consecutive runs. Thank you for that idea bitplane, it has solved my problem :lol:
Post Reply