[C#] System.AccessViolationException at OverlayManager.addOverlayElementFactory() Topic is solved

Problems building or running the engine, queries about how to use features etc.
Post Reply
dunklemateria
Gnoblar
Posts: 7
Joined: Sun May 16, 2021 8:11 am

[C#] System.AccessViolationException at OverlayManager.addOverlayElementFactory()

Post by dunklemateria »

Ogre Version: 1.12.12 (Rhagorthua)
Operating System: Windows Version 10.0.19041.985
Render System: D3D9 : Created D3D9 Rendering Window

Hello,

I'm new to Ogre3d and fiddle arround with the basics. I deciced to use the .Net bindings to write my code in C#. My code initialized a window, created scene manager, camera, view port and it worked well. Now I tried to implement a custom OverlayElementFactory by extending class org.ogre.PanelOverlayElementFactory and register it at OverlayManger. Any time the following code is executed:

Code: Select all

OverlayManager.getSingleton().addOverlayElementFactory(new CompassOverlayFactory());
I get a System.AccessViolationException with the following stack trace:

Code: Select all

   at org.ogre.OgreOverlayPINVOKE.OverlayManager_addOverlayElementFactory(HandleRef jarg1, HandleRef jarg2)
   at org.ogre.OverlayManager.addOverlayElementFactory(OverlayElementFactory elemFactory)
   at SharpIris.LuaInterface.Ogre.Factories.CompassOverlay.RegisterFactory() in ...\\Ogre\\Factories\\CompassOverlay.cs:line 44
   at SharpIris.LuaInterface.Ogre.Wrapper.RegisterFactories() in ...\\Ogre\\Wrapper.cs:line 336
   at SharpIris.LuaInterface.Ogre.Wrapper.CreateWindow(String windowTitle) in ...\\Ogre\\Wrapper.cs:line 330
   at SharpIris.LuaInterface.Ogre.OgreMain.InitOgre(String windowTitle, String pluginPath, String baseDir, Boolean autoCreateWindow) in ...\\Ogre\\OgreMain.cs:line 51
It also does not matter, which factory class I want to register, it's also happing using the following code:

Code: Select all

OverlayManager.getSingleton().addOverlayElementFactory(new PanelOverlayElementFactory ());
Do in need to initialize or register any other element or class first before try registering an OverlayElementFactory?

Best regards
paroj
OGRE Team Member
OGRE Team Member
Posts: 1993
Joined: Sun Mar 30, 2014 2:51 pm
x 1073
Contact:

Re: [C#] System.AccessViolationException at OverlayManager.addOverlayElementFactory()

Post by paroj »

the issue is likely that your ElementFactory is garbage collected, while still being used by Ogre.

The issue is that OverlayManager::addOverlayElementFactory does not take ownership of the pointer you pass to it. As it takes a raw C pointer the bindings also have no idea that the C# reference count should be updated.

Try keeping a your ElementFactory referenced in C# (e.g. store it as a class field).
dunklemateria
Gnoblar
Posts: 7
Joined: Sun May 16, 2021 8:11 am

Re: [C#] System.AccessViolationException at OverlayManager.addOverlayElementFactory()

Post by dunklemateria »

You are Genius! I never thought of that! Thank you very much!
dunklemateria
Gnoblar
Posts: 7
Joined: Sun May 16, 2021 8:11 am

Re: [C#] System.AccessViolationException at OverlayManager.addOverlayElementFactory()

Post by dunklemateria »

Sorry to write that, but I tried to implement it using static and non-static fields, but the results are the same. I'll try now without factories and create it using OverlayManager.create() and then add it manually to Overlay object.

I'd love to read another hint and give it a try because using factories is much better way.

Thank you anyways.

EDIT: Creating Overlay using e.g. OverlayManager.getSingleton().create("Compass") results in a System.AccessViolationException, too.

EDIT2: There are more System.AccessViolationException in case of activating RenderSystem_GL plugins.cfg at Finalizer during application shutdown.
Last edited by dunklemateria on Mon May 17, 2021 4:02 pm, edited 3 times in total.
paroj
OGRE Team Member
OGRE Team Member
Posts: 1993
Joined: Sun Mar 30, 2014 2:51 pm
x 1073
Contact:

Re: [C#] System.AccessViolationException at OverlayManager.addOverlayElementFactory()

Post by paroj »

can you post more of your code?

adding this to https://github.com/OGRECave/ogre/blob/m ... example.cs

Code: Select all

        var fac = new PanelOverlayElementFactory();
        scnMgr.addRenderQueueListener(OverlaySystem.getSingleton());
        OverlayManager.getSingleton().addOverlayElementFactory(fac);
        OverlayManager.getSingleton().create("PanelOverlayElement");
        System.GC.SuppressFinalize(fac);
works for me.

note that, I had to change the approach to System.GC.SuppressFinalize, as the OverlayManager actually tries to delete the factory on shutdown.
dunklemateria
Gnoblar
Posts: 7
Joined: Sun May 16, 2021 8:11 am

[Solved][C#] System.AccessViolationException at OverlayManager.addOverlayElementFactory()

Post by dunklemateria »

Thank you,

I was now able to find my error. My class did not extend org.ogre.ApplicationContext, neither implemented initApp() nor setup(). It used new Root() to create a root context. Now I managed to extend ApplicationContext and registering and creating Overlays are working correctly.
Post Reply