[BUG] TextureUnitState ensurePrepared & ensureLoaded

Discussion area about developing or extending OGRE, adding plugins for it or building applications on it. No newbie questions please, use the Help forum for that.
User avatar
0xC0DEFACE
OGRE Expert User
OGRE Expert User
Posts: 84
Joined: Thu May 21, 2009 4:55 am
x 7

[BUG] TextureUnitState ensurePrepared & ensureLoaded

Post by 0xC0DEFACE »

Hi.

I found and fixed this bug a while ago, however was reminded of it when upgrading to 1.8.0.

The bug is a crash that can occur in OgreTextureUnitState.cpp in the ensurePrepared and ensureLoaded function.

Repro:
Create a resource group.
Load some resources in it.
Destroy the resource group.
Create a new resource group with a different name.
Load the same set of resources in the new resource group.

The texture unit state will try to load itself even though its original resource group no longer exists, causing a crash.

The solution is to destroy the texture unit state if the resource groups dont match, making the texture unit state get recreated rather than get reloaded.

Here is a patch for the fix:

Code: Select all

diff -r dac6bfcd3327 OgreMain/src/OgreTextureUnitState.cpp
--- a/OgreMain/src/OgreTextureUnitState.cpp	Sun Sep 16 20:47:24 2012 -0500
+++ b/OgreMain/src/OgreTextureUnitState.cpp	Thu Sep 20 13:43:24 2012 +1000
@@ -1146,6 +1146,9 @@
 	{
 		if (!mFrames[frame].empty() && !mTextureLoadFailed)
 		{
+			if ( !mFramePtrs[frame].isNull() && mParent->getResourceGroup() != mFramePtrs[frame]->getGroup() )
+				mFramePtrs[frame].setNull();
+
 			// Ensure texture is loaded, specified number of mipmaps and
 			// priority
 			if (mFramePtrs[frame].isNull())
@@ -1178,6 +1181,9 @@
 	{
 		if (!mFrames[frame].empty() && !mTextureLoadFailed)
 		{
+			if ( !mFramePtrs[frame].isNull() && mParent->getResourceGroup() != mFramePtrs[frame]->getGroup() )
+				mFramePtrs[frame].setNull();
+
 			// Ensure texture is loaded, specified number of mipmaps and
 			// priority
 			if (mFramePtrs[frame].isNull())

Thanks!!