Errors when reinitializing Mogre

juharkonen

03-11-2008 14:45:26

I'm not sure if this is Ogre or Mogre issue but I decided to post to the Mogre forums as I'm using Mogre and have not tried to reconstruct the errors with Ogre.

My application is trying to start Mogre, close it and then start it again. However I get an AccessViolationException if I don't dispose the ResourceGroupManager singleton manually, and a NullReferenceException from TextureManager when I try to set the default number of mipmaps.


I understood that disposing the ogre root object was enough as most of the samples only dispose the root. I suspect that the problem may be caused by singletons and/or static members not being initialized properly after reinitialization. Here's a little program for reconstructing the problem (you have to add references and setup ogre resources accordingly to run it). I have tested this with both OpenGL and Direct3D renderers and they behave identically.



class Program
{
static void Main(string[] args)
{
// Initialize ogre
Root root = new Root();
Config(root);
InitializeResources();
root.Initialise(true);

TextureManager.Singleton.DefaultNumMipmaps = 5;

ResourceGroupManager.Singleton.InitialiseAllResourceGroups();

// ResourceGroupManager has to be disposed manually in order to avoid
// AccessViolationException later on when calling ResourceGroupManager.Singleton.AddResourceLocation
ResourceGroupManager.Singleton.Dispose();

// Dispose the root
root.Shutdown();
root.Dispose();


// Create new root and initialize again
root = new Root();

Config(root);

// Call to ResourceGroupManager.Singleton.AddResourceLocation will cause
// an AccessViolationException if ResourceGroupManager was not disposed
InitializeResources();
root.Initialise(true);

// Attempt to set the number of mipmaps causes NullReferenceException from inside Mogre.TextureManager.get_DefaultNumMipmaps.
if (TextureManager.Singleton != null)
{
TextureManager.Singleton.DefaultNumMipmaps = 5;
}

root.Dispose();
}

private static void Config(Root root)
{
// Try to load settings from config file
//if (root.RestoreConfig() == false)
{
// Failed to load - show dialog
if (root.ShowConfigDialog() == false)
{
// User cancelled
//return false;
}
}
}

private static void InitializeResources()
{
// Load resource paths from config file
ConfigFile cf = new ConfigFile();
cf.Load("resources.cfg", "\t:=", true);

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

String secName, typeName, archName;

// Normally we would use the foreach syntax, which enumerates the values, but in this case we need CurrentKey too;
while (seci.MoveNext())
{
secName = seci.CurrentKey;
ConfigFile.SettingsMultiMap settings = seci.Current;
foreach (KeyValuePair<string, string> pair in settings)
{
typeName = pair.Key;
archName = pair.Value;
ResourceGroupManager.Singleton.AddResourceLocation(archName, typeName, secName);
}
}
}

}


Any idea if it is possible to do reinitialization with Mogre?

feanor91

05-11-2008 05:01:54

Hi,

From my own investigation, you'r totaly right, problem is caused by singleton (search topic about reflection problem from me). Unfortunatly, this kind of problem seems to interest nobody, because I have no answers yet. If you debug the code deeper in OGRE source, you will see this is a singleton problem, but it's all that I understand and don't know how to solve it.

Edit ; here the thread I talked : http://www.ogre3d.org/phpBB2addons/viewtopic.php?t=8018

Good luck. And if you found something, let me know. For my part, I start on another kind of problem (playing with animations...)

juharkonen

05-11-2008 13:13:59

Hi feanor and thanks for your reply. I found out that ogre seems to work fine after reinitialization if I do dispose the ResourceGroupManager manually and don't set the default mipmaps after reinitializing ogre. However this leaves me suspicious that there may remain something that doesn't work properly and could cause trouble in some usage scenarios.

feanor91

05-11-2008 17:58:25

OK, and how do you free resource mannager?