Error While Loading File

Problems building or running the engine, queries about how to use features etc.
Post Reply
ART_Adventures
Goblin
Posts: 217
Joined: Sat Apr 16, 2005 4:47 pm

Error While Loading File

Post by ART_Adventures »

I have been searching through the DotScene Loader which demonstrates how to load a file using the OGRE Resource Manager, but it crashes every time my function (LoadFromFile) completes (not during any of the function calls). Just after it returns false it hits an unhandled exception, where upon the debugger then takes me to a function in free.c. Can anybody help. Here is the code:

Code: Select all

#ifndef _GRAPH_H_
#define _GRAPH_H_

#include "GraphNode.h"
#include "GraphEdge.h"

template <class node_type, class edge_type>
class cSparseGraph
{
private:
protected:
public:

	typedef edge_type	EdgeType;
	typedef node_type	NodeType;

	typedef std::vector<node_type> NodesVector;
	typedef std::vector<edge_type> EdgesVector;

	NodesVector m_Nodes;
	EdgesVector m_Edges;

	bool LoadFromFile(std::string Path)
	{
		DataStreamPtr pStream = ResourceGroupManager::getSingleton().
		openResource(Path);
		String data = pStream->getAsString();
		pStream->close();
		pStream.setNull();
		return false;
	}
};

#endif
Thanks in advance.
ART_Adventures
Goblin
Posts: 217
Joined: Sat Apr 16, 2005 4:47 pm

Post by ART_Adventures »

I have a feeling this is related to template classes and singleton objects?
Scrolllock
Gnoblar
Posts: 15
Joined: Sat Mar 18, 2006 8:33 pm
Location: South Africa
Contact:

Post by Scrolllock »

I think I have the same problem.
Here is a minimal case:

Code: Select all

void DotScene::parse( const String &SceneName)
{
  DataStreamPtr pStream = ResourceGroupManager::getSingleton().
      openResource(SceneName,"General");
  String data =  pStream->getAsString();
  return;
}
When removing the second statement in the function, the crash goes away.

Code: Select all

void DotScene::parse( const String &SceneName)
{
   DataStreamPtr pStream = ResourceGroupManager::getSingleton().
       openResource(SceneName,"General");

   //String data =  pStream->getAsString();
   return;
}
Strange thing is when running in debug mode (F5), the program stops in ogrememorymanager.h, on this inline function

Code: Select all

inline void operator delete(void *reportedAddress)
{
    Ogre::MemoryManager::instance().op_del_sc( reportedAddress, gProcessID );    
}
but it runs and completes without crashing when execution continues after the "break".
Seems to be related to the disposing of the string memory when the stack unwinds. Any ideas how to fix it?
Last edited by Scrolllock on Sat Apr 22, 2006 9:21 pm, edited 1 time in total.
User avatar
pjcast
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 2543
Joined: Fri Oct 24, 2003 2:53 am
Location: San Diego, Ca
x 2
Contact:

Post by pjcast »

Sounds like an odd issue.. You should make sure your not overwritting the bounds of an array somewhere, or using null pointers, as these things cause these odd behaviors.

IIRC, no reason for that to crash, as you should get an exception if the resource wasn't found, and just converting the buffer to a string is not error prone and has been well tested.
Have a question about Input? Video? WGE? Come on over... http://www.wreckedgames.com/forum/
Scrolllock
Gnoblar
Posts: 15
Joined: Sat Mar 18, 2006 8:33 pm
Location: South Africa
Contact:

Post by Scrolllock »

I think the problem could be something to do with a compiler setting of the less obvious kind (at least to me). This is my first attempt at an Ogre app that uses the "PlugIn_DotSceneManager". Unfortunately I also upgraded to MSVC8.0 and the new Ogre before trying to load and render octree scene I converted from a ".scene" file.

Nonetheless, the problem does not seem to be related to the plug-in. I have now recreated the problem in the main function ... should eliminate those overwrite problems and such.

This program executes as expected -- it loads the xml file and prints it to the log. However, before exiting (i.e. on stack unwind), the crash I described earlier occurs.

And here follows the code:

Code: Select all

INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
{
	Ogre::Root * r = new Ogre::Root("plugins.cfg","display.cfg","log.txt");
    // Load resource paths from config file
    ConfigFile cf;
    cf.load("resources.cfg");

    // Go through all sections & settings in the file
    ConfigFile::SectionIterator seci = cf.getSectionIterator();

    String secName, typeName, archName;
    while (seci.hasMoreElements())
    {
        secName = seci.peekNextKey();
        ConfigFile::SettingsMultiMap *settings = seci.getNext();
        ConfigFile::SettingsMultiMap::iterator i;
        for (i = settings->begin(); i != settings->end(); ++i)
        {
            typeName = i->first;
            archName = i->second;
            ResourceGroupManager::getSingleton().addResourceLocation(
                archName, typeName, secName);
        }
    }
	DataStreamPtr pStream = 
			ResourceGroupManager::getSingleton().openResource("xmlFile.txt");

	String data = pStream->getAsString();
	LogManager::getSingleton().logMessage(data);

	return 0;	
}
User avatar
pjcast
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 2543
Joined: Fri Oct 24, 2003 2:53 am
Location: San Diego, Ca
x 2
Contact:

Post by pjcast »

I'm surprised you get a valid file back, I am pretty sure you should be initializing the (or all) resource groups prior to using them :?
Have a question about Input? Video? WGE? Come on over... http://www.wreckedgames.com/forum/
Scrolllock
Gnoblar
Posts: 15
Joined: Sat Mar 18, 2006 8:33 pm
Location: South Africa
Contact:

Post by Scrolllock »

sure you should be initializing the (or all) resource groups
The first three lines now look like this:

Code: Select all

INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
{
	Ogre::Root * r = new Ogre::Root("plugins.cfg","display.cfg","log.txt");
	ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
Alas, no change in the program's behaviour, the problem persists.
User avatar
jacmoe
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 20570
Joined: Thu Jan 22, 2004 10:13 am
Location: Denmark
x 179
Contact:

Post by jacmoe »

The initialiseAllResourceGroups should be after adding all resource locations.
/* Less noise. More signal. */
Ogitor Scenebuilder - powered by Ogre, presented by Qt, fueled by Passion.
OgreAddons - the Ogre code suppository.
Scrolllock
Gnoblar
Posts: 15
Joined: Sat Mar 18, 2006 8:33 pm
Location: South Africa
Contact:

Post by Scrolllock »

The initialiseAllResourceGroups should be after adding all resource locations.
This does change things. I now get a more concrete crash :cry:
Access violation reading location 0x0000005c.
This happens on initialiseAllResourceGroups.

And here is the new updated code:

Code: Select all

INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
{
	Ogre::Root * r = new Ogre::Root("plugins.cfg","display.cfg","log.txt");
    // Load resource paths from config file
    ConfigFile cf;
    cf.load("resources.cfg");

    // Go through all sections & settings in the file
    ConfigFile::SectionIterator seci = cf.getSectionIterator();
    String secName, typeName, archName;
    while (seci.hasMoreElements())
    {
        secName = seci.peekNextKey();
        ConfigFile::SettingsMultiMap *settings = seci.getNext();
        ConfigFile::SettingsMultiMap::iterator i;
        for (i = settings->begin(); i != settings->end(); ++i)
        {
            typeName = i->first;
            archName = i->second;
            ResourceGroupManager::getSingleton().addResourceLocation(
                archName, typeName, secName);
        }
    }
	ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
	DataStreamPtr pStream = 
			ResourceGroupManager::getSingleton().openResource("xmlFile.txt");

	String data = pStream->getAsString();
	LogManager::getSingleton().logMessage(data);

	return 0;	
}
User avatar
jacmoe
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 20570
Joined: Thu Jan 22, 2004 10:13 am
Location: Denmark
x 179
Contact:

Post by jacmoe »

If you are loading any resources with images, referenced through material scripts, you need to construct a window, create a rendersystem and a scenemanager, before the initialising all resourcegroups. :)
/* Less noise. More signal. */
Ogitor Scenebuilder - powered by Ogre, presented by Qt, fueled by Passion.
OgreAddons - the Ogre code suppository.
Scrolllock
Gnoblar
Posts: 15
Joined: Sat Mar 18, 2006 8:33 pm
Location: South Africa
Contact:

Post by Scrolllock »

I see my trivial 'test-case' program is becoming more like an interactive tutorial. Thanks, I can use some of those :)
you need to construct a window, create a rendersystem and a scenemanager, before the initialising all resourcegroups
Below you'll find a new incarnation of the code. Still produces the same problem.

Code: Select all

	Ogre::Root * r = new Ogre::Root("plugins.cfg","display.cfg","log.txt");
	Ogre::RenderSystemList *rsList = r->getAvailableRenderers();
	Ogre::RenderSystemList::iterator it = rsList->begin();
	const char * STR_DX_SYSTEM = "Direct3D9 Rendering Subsystem";
	while (it != rsList->end())
	{
		if ((*it)->getName().compare(STR_DX_SYSTEM) == 0)
			break;
		it++;
	}
	if (it == rsList->end())
		return;

	r->setRenderSystem(*it);
	(*it)->setConfigOption("Full Screen","No");  
	(*it)->setConfigOption("Video Mode","800 x 600 @ 16-bit colour");
	Ogre::RenderWindow * w = r->initialise(true,"Some Window Title");

	r->createSceneManager("DotSceneOctreeManager");
    // Load resource paths from config file
    ConfigFile cf;
    cf.load("resources.cfg");

    // Go through all sections & settings in the file
    ConfigFile::SectionIterator seci = cf.getSectionIterator();
    String secName, typeName, archName;
    while (seci.hasMoreElements())
    {
        secName = seci.peekNextKey();
        ConfigFile::SettingsMultiMap *settings = seci.getNext();
        ConfigFile::SettingsMultiMap::iterator i;
        for (i = settings->begin(); i != settings->end(); ++i)
        {
            typeName = i->first;
            archName = i->second;
            ResourceGroupManager::getSingleton().addResourceLocation(
                archName, typeName, secName);
        }
    }
	ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
	DataStreamPtr pStream = 
			ResourceGroupManager::getSingleton().openResource("xmlFile.txt");

	String data = pStream->getAsString();
	LogManager::getSingleton().logMessage(data);
BTW, I also tried moving down

Code: Select all

Ogre::RenderWindow * w = r->initialise(true,"Some Window Title");
to replace this line

Code: Select all

ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
Scrolllock
Gnoblar
Posts: 15
Joined: Sat Mar 18, 2006 8:33 pm
Location: South Africa
Contact:

Post by Scrolllock »

It may be that my problem is related to the problem described here:
http://www.ogre3d.org/phpBB2/viewtopic.php?t=6914
DMDevPaulLaska wrote:My error occurs when an Ogre::String is assigned the results from Ogre::ConfigFile::getSettings(), which I believe creates a temporary String variable before doing the assignment. So far I've been able to figure out that for some reason the temporary String variable is getting created without using Ogre's MemoryManagement (i.e. it doesn't use the overloaded new operator), but when it cleans up the memory it calls the overloaded delete operator in Ogre's MemoryManagement. The assert to make sure that Ogre's MemoryManagement allocated the memory is where it fails.
In this case, the culprit member is:

Code: Select all

DataStreamPtr::getAsString()
So, I went to the online CVS, had a peek at the function:

Code: Select all

    String DataStream::getAsString(void)
    {
        // Read the entire buffer
        char* pBuf = new char[mSize+1];
        read(pBuf, mSize);
        pBuf[mSize] = '\0';
        String str;
        str.insert(0, pBuf, mSize);
        delete [] pBuf;
        return str;
    }
And, going back to the "example code" of the previous post, I replaced this line:

Code: Select all

String data = pStream->getAsString();
with these:

Code: Select all

    size_t size = pStream->size();
    char* pBuf = new char[size+1];
    pStream->read(pBuf, size);
    pBuf[size] = '\0';
    String data;
    data.insert(0, pBuf, size);
    delete [] pBuf;
And lo and behold, the crash is gone :shock: But, hey, I copied the code from the library, so what could be the cause :?

I used the binary download of RC2 -- could that be the problem?
Scrolllock
Gnoblar
Posts: 15
Joined: Sat Mar 18, 2006 8:33 pm
Location: South Africa
Contact:

Post by Scrolllock »

After getting all the source and compiling the library, the code crashes here:

Code: Select all

           
 // If you hit this assert, you tried to deallocate RAM that wasn't 
 // allocated by this memory manager.
            m_assert(au != NULL);
            if (au == NULL) 
                throw "Request to deallocate RAM that was never allocated";
User avatar
jacmoe
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 20570
Joined: Thu Jan 22, 2004 10:13 am
Location: Denmark
x 179
Contact:

Post by jacmoe »

That probably means that you need to set it up to use a shared runtime multithreaded DLL. :wink:
/* Less noise. More signal. */
Ogitor Scenebuilder - powered by Ogre, presented by Qt, fueled by Passion.
OgreAddons - the Ogre code suppository.
Scrolllock
Gnoblar
Posts: 15
Joined: Sat Mar 18, 2006 8:33 pm
Location: South Africa
Contact:

[Solved] Error While Loading File

Post by Scrolllock »

At last!
Thank you jacmoe.
Switching from

Code: Select all

Multi-threaded Debug (/MTd)
to

Code: Select all

Multi-threaded Debug DLL (/MDd)
Solves the problem :D
User avatar
leandra
Kobold
Posts: 30
Joined: Tue Feb 21, 2006 7:07 pm

Re: [Solved] Error While Loading File

Post by leandra »

Scrolllock wrote:At last!
Thank you jacmoe.
Switching from

Code: Select all

Multi-threaded Debug (/MTd)
to

Code: Select all

Multi-threaded Debug DLL (/MDd)
Solves the problem :D
hi,
I had this same problem but fix it like said,
but when I created another function to the class it started again
and dont know why, because I didnt touch that code :?
Scrolllock
Gnoblar
Posts: 15
Joined: Sat Mar 18, 2006 8:33 pm
Location: South Africa
Contact:

Post by Scrolllock »

You have to set the multi-thread option for the release build as well. Check your settings against the wiki:
http://www.ogre3d.org/wiki/index.php/Se ... .2B.2B.Net.
leandra wrote:but when I created another function to the class it started again
Maybe posting some of the code could help.
AndreB
Gnoblar
Posts: 6
Joined: Fri Apr 21, 2006 8:52 pm

Post by AndreB »

Well, I have the same problem..

First I used some code I hacked together pretty quickly - everything ran fine. I then started splitting the code up, not unlike the gamestate-tutorial does it.

That's when the errors started - namely thrown by the ResourceGroupManager and the InputReader.

Since those functions are closely related to the only "Singleton"-like stuff I built in yet, I guess that's causing the error.

I wasn't able to build that Singleton-Feature into my class as well - I am still learning some aspects of C++/etc. - so I guess it's theoretically possible to have TWO ResourceGroupManagers. Not that I do - but I could have, by creating two instances of the "game"-class that I created and where I encapsuled the Ogre-Stuff.

Creating the InputReader as well as adding ResourceDirs doesn't cause any problems, but capturing the input as well as initialising the directories does.
I didn't have any of those problems when all my stuff was still in one big function (the main-one). I guess you did not ever get that error because you never tried putting ResourceManagers or something similir inside of classes that might be instanced more than once?

Setting that "multi threaded DLL"-option wouldn't do me any good, since it had been activated from start (used that wizard). That includes Release-mode.

Some code in case I didn't manage to make myself clear above..

Code: Select all


// taken from header.h

class Game
{
public:
	Game();
	~Game();

	void start();
	void setupResFolders();
	void mainLoop();

protected:
	
	float timeSinceLast;

	Ogre::Root* mRoot;
	Ogre::RenderWindow* mWindow;
	Ogre::SceneManager* mSceneMgr;
	Ogre::Viewport* mViewport;
	Ogre::InputReader* mInputReader;


// taken from game.cpp

void Game::setupResFolders()
{
	Ogre::ConfigFile mConfigFile;
	mConfigFile.load("resources.cfg");
	
    // Go through all sections & settings in the file
    Ogre::ConfigFile::SectionIterator mSectionIterator = mConfigFile.getSectionIterator();
	
    Ogre::String secName, typeName, archName;
    while (mSectionIterator.hasMoreElements())
    {
		secName = mSectionIterator.peekNextKey();
        Ogre::ConfigFile::SettingsMultiMap *mSettingsMultiMap = mSectionIterator.getNext();
        Ogre::ConfigFile::SettingsMultiMap::iterator mIterator;
        for (mIterator = mSettingsMultiMap->begin(); mIterator != mSettingsMultiMap->end(); ++mIterator)
        {
			typeName = mIterator->first;
            archName = mIterator->second;
            Ogre::ResourceGroupManager::getSingleton().addResourceLocation(archName, typeName, secName);
        } // of for
	} // of while
	
	// Initialise all Resource-Folders
	Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups(); // crashes right here

}
Commenting out the ResourceGroupManager-initialise-thingie, the second crash is caused by...

Code: Select all

// taken from game.cpp, void game::start()

// Set up Window
    RenderWindow* mRenderWindow;
	mRenderWindow = mRoot->initialise(true,"Awesome Game");

	// create and choose Scene-Manager
	SceneManager* mSceneMgr;
	mSceneMgr = mRoot->createSceneManager(ST_GENERIC);

	// create InputReader
	InputReader* mInputReader = PlatformManager::getSingleton().createInputReader();
    mInputReader->initialise(mRenderWindow); 

	//start mainLoop
	mainLoop();


//
// somewhat further down in game.cpp ... 

void Game::mainLoop()
{
	// mainLoop here
	while (true)
	{
		timeSinceLast = UpdateTime();
		//run = 1;
		mInputReader->capture(); // crashes right here
Please try to overlook any serious insults to OO-programming that I might have done ;-)
Scrolllock
Gnoblar
Posts: 15
Joined: Sat Mar 18, 2006 8:33 pm
Location: South Africa
Contact:

Post by Scrolllock »

AndreB, maybe your mistake is the one jacmoe pointed out to me:
jacmoe wrote:If you are loading any resources with images, referenced through material scripts, you need to construct a window, create a rendersystem and a scenemanager, before the initialising all resourcegroups.
AndreB
Gnoblar
Posts: 6
Joined: Fri Apr 21, 2006 8:52 pm

Post by AndreB »

I deleted that line in my code-snippets, but I already tried initialising the stuff right before calling mainLoop(). It caused the very same crash :-(

But I will give it another try this evening.
AndreB
Gnoblar
Posts: 6
Joined: Fri Apr 21, 2006 8:52 pm

Post by AndreB »

Hi there, I am back with new test results.

Moving the ResGroupManager.initialise right before the mainLoop() did indeed solve that error.

I guess I thought it did not because the error thrown by the inputManager looks exactly the same. Fact is, the error is solved, only to be replaced by the inputManager throwing an exception.

Shall I create a new thread with that problem? I don't know how the original problem and that one might be related.

The code in question..

Code: Select all

// create InputReader
	InputReader* mInputReader = PlatformManager::getSingleton().createInputReader();
    mInputReader->initialise(mRenderWindow); 


	Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups(); // now working

	//start mainLoop
	mainLoop();
further down..

Code: Select all

void Game::mainLoop()
{
	// mainLoop here
	while (true)
	{
		timeSinceLast = UpdateTime();
		//run = 1;
		mInputReader->capture(); // crashing right here
/EDIT

typos fixed
Scrolllock
Gnoblar
Posts: 15
Joined: Sat Mar 18, 2006 8:33 pm
Location: South Africa
Contact:

Post by Scrolllock »

I guess it is best to start a new thread.
Post Reply