Weird exceptions

cutterslade

13-06-2006 00:20:59

Hey guys, I'm having some really weird problems. My application is divided in two classes:


using System;
using System.Collections.Generic;
using OgreDotNet;
using OgreDotNet.Cegui;
using CeguiDotNet;
using Math3D;

namespace OgreTest
{
/// <summary>
/// Description of Engine.
/// </summary>
public class Engine
{
Root mRoot;
LogManager mLogManager;

//Variables used to choose the render system
RenderSystem mRenderSystem;

//Ogre "must haves"
RenderWindow mRenderWindow;
SceneManager mSceneManager;
Camera mCamera;
Viewport mViewport;

OgreCEGUIRenderer mGuiRenderer;
GuiSystem mGuiSystem;

public Window background;

public void Initialise()
{
mRoot = new Root("plugins.cfg", "display.cfg", "HelloOgreDotNetv2.txt");

mLogManager = LogManager.GetSingleton();

//Set the RenderingSystem
mRenderSystem = mRoot.GetRenderSystemByName("Direct3D9 Rendering Subsystem");
mRoot.SetRenderSystem(mRenderSystem);

//Set the Config Opts for the RenderSystem.
mRenderSystem.SetConfigOption("Allow NVPerfHUD", "No");//"Yes"
mRenderSystem.SetConfigOption("Anti aliasing","None");//
mRenderSystem.SetConfigOption("Floating-point mode","Fastest");//Consistant
mRenderSystem.SetConfigOption("Full Screen","No");
mRenderSystem.SetConfigOption("VSync","No");//"Yes"
mRenderSystem.SetConfigOption("Video Mode","640 x 480 @ 32-bit colour");

//Initialize Root
mRenderWindow = mRoot.Initialise(true, "Hello OGRE.NET Part II");

//Create SceneManager
mSceneManager = mRoot.CreateSceneManager((ushort)SceneType.Generic);

//Create & Setup Camera
mCamera = mSceneManager.CreateCamera("SceneCamera");
mCamera.SetPosition(new Vector3(0, 0, 500));
mCamera.SetNearClipDistance(5);

// Create one Viewport, entire window
mViewport = mRenderWindow.AddViewport(mCamera);
mViewport.SetBackgroundColour(System.Drawing.Color.Black);

// Alter the camera aspect ratio to match the viewport
mCamera.SetAspectRatio(mViewport.GetWidth() / mViewport.GetHeight());

string path = "c:/programacao/ogresdk/media";

ResourceGroupManager.getSingleton().addResourceLocation(path, "FileSystem", "General");
ResourceGroupManager.getSingleton().addResourceLocation(path + "/materials/programs", "FileSystem", "General");
ResourceGroupManager.getSingleton().addResourceLocation(path + "/materials/scripts", "FileSystem", "General");
ResourceGroupManager.getSingleton().addResourceLocation(path + "/materials/textures", "FileSystem", "General");
ResourceGroupManager.getSingleton().addResourceLocation(path + "/models", "FileSystem", "General");
ResourceGroupManager.getSingleton().addResourceLocation("../../media", "FileSystem", "General");
ResourceGroupManager.getSingleton().addResourceLocation("../../media/interface", "FileSystem", "General");

ResourceGroupManager.getSingleton().initialiseAllResourceGroups();

mGuiRenderer = new OgreCEGUIRenderer (mRenderWindow,(byte)RenderQueueGroupID.RENDER_QUEUE_OVERLAY, false, 3000, mSceneManager );
mGuiRenderer.Initialise();
mGuiSystem = new GuiSystem(mGuiRenderer);

SchemeManager.Instance.LoadScheme("WindowsLookSkin.scheme");
CeguiDotNet.FontManager.Instance.createFont("Commonwealth-10.font");
mGuiSystem.SetDefaultMouseCursor("WindowsLook", "MouseArrow");

background = WindowManager.Instance.CreateWindow("DefaultWindow", "BackgroundWindow");
mGuiSystem.GUISheet = background;

background.AddChildWindow(WindowManager.Instance.LoadWindowLayout("Demo8.layout"));
background.Show();

CeguiDotNet.Window w = WindowManager.Instance.getWindow("Demo8/ViewScroll");
Scrollbar scroll = new Scrollbar(Window.getCPtr(w).Handle, false);
scroll.setVisible(false);
scroll.SubscribeEvents();

scroll.ScrollPositionChanged += delegate(WindowEventArgs e)
{
WindowManager.Instance.getWindow("Demo8/Window1").SetPosition(0.2f,scroll.getScrollPosition());

return true;
};


}

public void Render()
{
mRoot.StartRendering();
}

public Root GetRoot()
{
return mRoot;
}

public SceneManager GetSceneManager()
{
return mSceneManager;
}

public RenderWindow GetRenderWindow()
{
return mRenderWindow;
}

public Camera GetCamera()
{
return mCamera;
}

public void Dispose()
{
mGuiSystem.Dispose();
mGuiRenderer.Dispose();
mRenderWindow.Dispose();
mRenderSystem.Dispose();
mRoot.Dispose();
}

}
}



using System;
using System.Collections.Generic;
using OgreDotNet;
using OgreDotNet.Cegui;
using CeguiDotNet;
using Math3D;

namespace OgreTest
{
class MainClass
{
public static void Main(string[] args)
{
//variables for quick, simple event handling.
OgreDotNet.EventHandler mEventHandler;
bool mDone = false;

Engine mainEngine = new Engine();

mainEngine.Initialise();

Entity levelMesh = mainEngine.GetSceneManager().CreateEntity("level","test.mesh");
SceneNode level = mainEngine.GetSceneManager().GetRootSceneNode().CreateChildSceneNode("MapNode");
level.AttachObject(levelMesh);

Entity mesh = mainEngine.GetSceneManager().CreateEntity("robot","robot.mesh");
SceneNode robot = mainEngine.GetSceneManager().GetRootSceneNode().CreateChildSceneNode("RobotNode");
robot.AttachObject(mesh);
robot.Translate(0,400,0);

mainEngine.GetSceneManager().SetSkyDome(true, "Examples/CloudySky", 5, 8);

//Setup a minimal eventhandler using anonymous methods.
float moveX = 0, moveY = 0;
mEventHandler = new OgreDotNet.EventHandler(mainEngine.GetRoot(), mainEngine.GetRenderWindow());
mEventHandler.SubscribeEvents();


mEventHandler.FrameStarted += delegate(FrameEvent e)
{
if (mainEngine.GetRenderWindow().Closed || mDone) return false;
return true;
};
mEventHandler.FrameEnded += delegate(FrameEvent e)
{

robot.Roll(2);
robot.Yaw(-1.2f);
mainEngine.GetCamera().Move(new Math3D.Vector3(moveX,0,moveY));
mainEngine.GetCamera().LookAt = robot.GetPosition();

return true;
};
mEventHandler.KeyClicked += delegate(KeyEvent e)
{
if (e.KeyCode == KeyCode.Escape) mDone = true;

};
mEventHandler.KeyPressed += delegate(KeyEvent e)
{
if (e.KeyCode == KeyCode.Up) moveY = -4;
if (e.KeyCode == KeyCode.Down) moveY = 4;
if (e.KeyCode == KeyCode.Left) moveX = -4;
if (e.KeyCode == KeyCode.Right) moveX = 4;
};
mEventHandler.KeyReleased += delegate(KeyEvent e)
{
moveX = moveY = 0;
};
mEventHandler.MouseMoved += delegate(MouseMotionEvent e)
{
GuiSystem.Instance.InjectMouseMove(e.DeltaX*400,e.DeltaY*400);

};
mEventHandler.MousePressed += delegate(MouseEvent e)
{
GuiSystem.Instance.InjectMouseButtonDown(MouseButton.Left);

};
mEventHandler.MouseReleased += delegate(MouseEvent e)
{
GuiSystem.Instance.InjectMouseButtonUp(MouseButton.Left);

};
mEventHandler.MouseDragged += delegate(MouseMotionEvent e)
{
GuiSystem.Instance.InjectMouseMove(e.DeltaX*400,e.DeltaY*400);

};

//Start the rendering loop
mainEngine.Render();

mEventHandler.Dispose();


mainEngine.Dispose();


}
}
}


It works fine, except after some mouse clicks (5 or 6 most of the time) the program crashes and a exception is generated.


Unhandled Exception: System.AccessViolationException: Attempted to read or write
protected memory. This is often an indication that other memory is corrupt.
at CeguiDotNet.CeguiBindingsPINVOKE.GuiSystem_InjectMouseButtonDown(HandleRef
jarg1, Int32 jarg2)
at CeguiDotNet.GuiSystem.InjectMouseButtonDown(MouseButton button)
at OgreTest.MainClass.<Main>b__6(MouseEvent e)
at OgreDotNet.EventHandler.MousePressedHandler(Single x, Single y, Single z,
Int32 buttonid)
at OgreDotNet.OgreBindingsPINVOKE.Root_StartRendering(HandleRef jarg1)
at OgreDotNet.Root.StartRendering()
at OgreTest.MainClass.Main(String[] args)


Also, when I press ESC the program finishes with an exception sometimes. What could be goin on?

rastaman

13-06-2006 04:51:30

try making your delegates acutal declared functions.

LuRenJia

12-08-2006 12:53:03

I have a same problem, sometimes VS2005 prompt a "CallbackOnCollectedDelegate".

My code is same with cutterslade's except using anonymous method as delegates. How confused!