Reloading Terrain.cfg problem

dannypb

11-06-2012 18:19:09

I'm having a problem loading a 2nd terrain scene. I'm getting protected memory errors, probably due to trying to create another terrain scene manager when one already exists. I have been able to load, delete and reload scenes with out terrain (space scenes). I can load a terrain scene fine the first time also. When I try to load a terrain scene a second time I'm getting the error. It seems to be a system error, because I'm not getting anything in the ogre.log file.

Here is my create Scene Code:
protected void CreateScene(string thisNewLoctid)
{
string input = "";
string[] getData;

//sceneInTransition = true;
InZone = false;

if (playid.Equals(""))
{
// initialize game start
GetPlayerData();
input = "SELECT loctid from entities where entyid = " + pentid;
getData = getDataRow(input);
Console.WriteLine("Location ID: " + getData[0]);
loctid = getData[0];
for (int i = 99; i >= 0; i--)
{
chatLines[i] = "";
}
//gSceneMgr = mRoot.CreateSceneManager(SceneType.ST_EXTERIOR_FAR, "GalaxyScene");
}
else
{
// clear out old scene
try
{

try
{
updateThread.Abort();
}
catch { }
Console.WriteLine("Update Thread Ended");
StaticEntities = new List<eEntities>();
DynamicEntities = new List<eEntities>();

LocationNode = null;
gSceneMgr.ClearScene();
//gSceneMgr.DestroyCamera(gCamera);
//gSceneMgr.DestroySceneNode("GalaxyScene");

Console.WriteLine("Scene Cleared");
mRenderWindow.RemoveAllViewports();
gCamera = null;
//GC.Collect();
}
catch (Exception e)
{
Console.WriteLine("Clear Scene Error: " + e.Message);
}
loctid = thisNewLoctid;
}
//get location and ent type
input = "SELECT if (a.etype > ' ', a.etype, b.typename) from entities a ";
input += "left outer join entitytype b on a.enttid = b.enttid ";
input += "where a.entyid = " + pentid;
getData = getDataRow(input);
Console.WriteLine("Entity Type: " + getData[0]);
if (getData[0].Equals("Capitol Ship")) { etype = CAPITOL_SHIP; } else
if (getData[0].Equals("Inside")) { etype = INSIDE; } else
if (getData[0].Equals("Outside")) { etype = OUTSIDE; } else
if (getData[0].Equals("Hover")) { etype = SURFACE_VEHICLE; } else
if (getData[0].Equals("Water")) { etype = WATER_VEHICLE; } else
if (getData[0].Equals("Air")) { etype = AIR_VEHICLE; } else
etype = FIGHTER;

input = "SELECT ltype from locations where loctid = " + loctid;
getData = getDataRow(input);
Console.WriteLine("Location Type: " + getData[0]);
if (getData[0].Equals("planet"))
{
ltype = SPACE;
if (etype == CAPITOL_SHIP) { locType = SPACE_WALK; }
else
locType = SPACE_FLIGHT;
gSceneMgr = mRoot.CreateSceneManager(SceneType.ST_EXTERIOR_REAL_FAR);
LocationNode = gSceneMgr.RootSceneNode.CreateChildSceneNode();
CreateNewton();
CreateSpaceCamera();
LoadSpaceLocation();
CreateSpaceOverlay();

}
else
{
ltype = GROUND;
if (etype == FIGHTER) { locType = GROUND_FLIGHT; }
else
if (etype == OUTSIDE) { locType = GROUND_WALK; }
else
locType = SPACE_WALK;
//gSceneMgr = mRoot.CreateSceneManager(SceneType.ST_EXTERIOR_FAR);
gSceneMgr = mRoot.CreateSceneManager(SceneType.ST_EXTERIOR_CLOSE);

LocationNode = gSceneMgr.RootSceneNode.CreateChildSceneNode();
CreateGroundCamera();
//CreateNewton();

LoadGroundLocation(
new Mogre.Vector3(5000.0f, 1000.0f, 5000.0f),
new Mogre.Quaternion(0.0f, 0.0f, 01.0f, 0.0f));
//gSceneMgr.SetFog(FogMode.FOG_EXP, fadeColour, 0.000001f * ((15000 - 20000) / 30));
CreateSpaceOverlay();

}

sceneInTransition = false;
InZone = true;

}


The actual error happens in the function CreateGroundTerrain called from LoadGroundLocation.

protected void CreateGroundTerrain()
{
try
{
string input = "SELECT * from locations where loctid = " + loctid;
string[] getData = getDataRow(input);

//gSceneMgr.Destroy();
try
{
gSceneMgr.SetWorldGeometry(getData[5]); // <------ this is where i get the error
SceneNodeName = gSceneMgr.RootSceneNode.Name;
Console.WriteLine("Scene Node Name: " + SceneNodeName);
}
catch (Exception e)
{
Console.WriteLine("Load Terrain Error loading " + getData[5] + " Error is: " + e.Message);
}
Console.WriteLine("Loading " + getData[5]);

if (getData[6].Length > 1)
{
Plane plane = new Plane(Mogre.Vector3.UNIT_Y, float.Parse(getData[7]));
MeshManager.Singleton.CreatePlane("water", ResourceGroupManager.DEFAULT_RESOURCE_GROUP_NAME,
plane, float.Parse(getData[8]), float.Parse(getData[8]), 20, 20, true,
1, 500, 500, Mogre.Vector3.UNIT_Z);

Entity ent = gSceneMgr.CreateEntity("WaterEntity", "water");
gSceneMgr.RootSceneNode.CreateChildSceneNode().AttachObject(ent);

ent.SetMaterialName(getData[6]);
ent.CastShadows = false;
}
}
catch (Exception e)
{
Console.WriteLine("Load Ground Location Error: " + e.Message);
}

}


The error message I get is: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. This last time I checked it took going back to the terrain scene 3 times before I got the error. I'm thinking it is that the world geometry object is being recreated every time, until I run out of memory, but I couldn't figure out how to delete it and start clean again at the start of the screen.

I appreciate any help or advice that you guys can give.


btw updates on my project are posted here: http://www.covenantofthephoenix.com/forums/topic/22416-game-ive-been-working-on-phoenix-run if anyone is curious.

Here are a couple pics: