"Handle is not initialized" exception

manski

12-06-2010 18:18:06

Hi,

I'm getting a strange exception when I tried to load an image using a stream:


System.InvalidOperationException was not handled.
Message=Handle is not initialized.
Source=mscorlib
StackTrace:
at System.Runtime.InteropServices.GCHandle.FromIntPtr(IntPtr value)
at gcroot<Mogre::ManagedDataStream ^>.->(gcroot<Mogre::ManagedDataStream ^>* ) in c:\program files (x86)\microsoft visual studio 9.0\vc\include\msclr\gcroot.h:line 120
at Mogre.DataStream_Proxy.read(DataStream_Proxy* , Void* buf, UInt32 count) in d:\devel\gamedevel\mogre\main\src\custom\manageddatastream.cpp:line 16
at Ogre.Image.load(Image* , SharedPtr<Ogre::DataStream>* , basic_string<char\,std::char_traits<char>\,std::allocator<char> >* )
at Mogre.Image.Load(DataStreamPtr stream, String type) in d:\devel\gamedevel\mogre\main\src\auto\mogreimage.cpp:line 161
at WoM.DynamicImage.CreateOgreImage() in D:\Programmieren\WoM\WoM\DynamicImage.cs:line 50
[...]
at WoM.WoWModelLoader..ctor(String p_modelFile) in D:\Programmieren\WoM\WoM\WoWModelLoader.cs:line 34
at WoM.CharViewer.SceneCreatingImpl(OgreWindow p_win) in D:\Programmieren\WoM\WoM\CharViewer.cs:line 82
at MogreFramework.OgreWindow.OnSceneCreating()
at MogreFramework.OgreWindow.InitializeOgre()
at MogreFramework.OgreWindow.Go()
at WoM.Program.Main() in D:\Programmieren\WoM\WoM\Program.cs:line 27
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:


The code for "CreateOgreImage()" (where the debugger notifies me about the exception) is:


public Mogre.Image CreateOgreImage() {
counter++;
Mogre.Image img = new Mogre.Image();
// NOTE: The format "tga" must be specified as otherwise Mogre will try to determine the format
// automatically which will result in Mogre "seeking" around in the file (which we don't support).
img.Load(new DataStreamPtr(new ManagedDataStream(new TgaFileStream(this))), "tga");
return img;
}


The idea behind this method (and behind the class "TgaFileStream") is to load a texture from a custom texture file format (which I must use and can't change). To be able to use it with Mogre (1.7.1 beta) is to decode it "manually" and then pipe the resulting image in form of a tga file to Mogre. This may not be the best solution but it was the easiest I could figure out.

The strange thing about this exception is: It only happens when this method is called while I'm loading my models. There are five textures loaded that way; but if I load them manually at the beginning of my program, the exception doesn't occur.

Any suggestions?

manski

12-06-2010 18:22:57

Hmmm... it seems to have something to do with the garbage collector. If I rewrite the code like this:


public Mogre.Image CreateOgreImage() {
Mogre.Image img = new Mogre.Image();
// NOTE: The format "tga" must be specified as otherwise Mogre will try to determine the format
// automatically which will result in Mogre "seeking" around in the file (which we don't support).
DataStreamPtr ptr = new DataStreamPtr(new ManagedDataStream(new TgaFileStream(this)));
img.Load(ptr, "tga");
return img;
}


it works suddenly.