System.AccessViolationException in OGRE3DRenderSystem

andihit

01-07-2009 22:47:27

Hi,

I want to use NxOgre in my MOGRE project, so I 've created a managed C++ DLL with the NxOgre-Physics-Stuff in it.
I followed the second BloodyMess tutorial, http://www.ogre3d.org/wiki/index.php/Bl ... Tutorial_2 and got stuck with it:

At that line:

OGRE3DRenderSystem* mRenderSystem = new OGRE3DRenderSystem(mScene);

Visual C++ 2008 Express always throws an System.AccessViolationException at this line :( (tried to write or read in protected memory, maybe ram broken, blah blah).

So the question is... what's wrong with that line? And better, how can I correct it? Any lib/dll missing?


P.S.: In this tutorial, in the class BloodyMessTutorial2 is a member mTimeController which gets assigned once and is never used again. In the BloodyMessTutorial2Listener class there is the same variable, but there it is used. So, is there any use of that variable in BloodyMessTutorial2? ;)

spacegaier

01-07-2009 23:02:43

Your P.S. is totally right. This declaration was a remaining from a former version where I used it.

I personally have no clue of Managed-C++ and such, but I could imagine that there is indeed a lib missing: Do you link to NxOgreOGRE3D.lib?

Another point could be that your mScene isn't valid for some reason. Checked that?

betajaen

01-07-2009 23:29:48

Yep, you link to NxOgreOGRE3D.lib but if you have some more troubles linking it legally - You can copy and paste the OGRE3DRenderSystem code in your application if the static lib is giving your trouble. The source for it is under the MIT licence, and I have no problems you treating it that way.

Perhaps even better - create a MOGRE version of it. ;)

andihit

02-07-2009 00:03:13

Do you link to NxOgreOGRE3D.lib?
Yes

Another point could be that your mScene isn't valid for some reason. Checked that?
Yes, the VC++ shows a pointer, and in mScene.mName there is that name ;-).


Yep, you link to NxOgreOGRE3D.lib but if you have some more troubles linking it legally - You can copy and paste the OGRE3DRenderSystem code in your application if the static lib is giving your trouble.
Yeah, but how can that code make troubles when I use it - I think it works for you and all others, then it have to work for me too ;) - it's only the tutorial, and if that wouldn't work, I would have found some posts like this now from other users :|.

The source for it is under the MIT licence, and I have no problems you treating it that way.
Thx, but I can't get it working either.

Perhaps even better - create a MOGRE version of it. ;)
Yeah, but first I want to use NxOgre a little but more than the tutorial :mrgreen:

Thanks for your fast replies.. but unfortunately the problem still exists :?


Some extra info I've discovered:
When I debug it in the C# Express (the main MOGRE game), the Visual C# debugger can debug the dll as well (I think cause I've used the debug-dll/libs now) --> and there the debugger stops at the same line (the debugger can fetch the cpp source -> Wow!), then if I forward one step/line in the debugger I get into the NxOgrePointerClass.h file, that function:
void* operator new(size_t size)
{
return ::NxOgre_Namespace::Memory::allocate(size, Ti);
}

one step ahead and I'm back at that line that makes the troubles... :shock:

Maybe memory can't be allocated? But I've enaugh free.

Any further ideas/solutions?
Thanks in advance

betajaen

02-07-2009 00:41:39

That's a more drastic error than I expected, and I'm dreading how to fix it.

For the purpose of testing, would you try comment out (or even remove) new and delete functions in the PointerClass; theoretically it should compile and work in C++ based programs. Try it in C# and see if it crashes.

Basically, my theory is since NxOgre allocates it's own memory instead of using the default new/delete operators, and C# likes manage its memory both solutions aren't compatible with each other.

andihit

02-07-2009 15:16:52

Now I've commented out the new and delete function, and also the "using .... ::operator new;" in many headerfiles (otherwise it won't compile).

In the C#-Debugger it gets stuck at the same line, and when I go one step ahead I'm at the same line.
In the C++-Debugger it hangs at the same line, one step ahead goes to:
mScene->createSceneGeometry(new NxOgre::PlaneGeometry(0, NxOgre::Real3(0, 1, 0)), NxOgre::Matrix44(NxOgre::Matrix44::IDENTITY));
and next step I'm in the Disassembly of f:\dd\vctools\crt_bld\self_x86\crt\prebuild\eh\managdeh.cpp code:
00000022 mov ecx,dword ptr [ebp-28h] , some steps ahead it goes further in the assembler code:

00000025 call dword ptr [ebp-24h]
00000028 nop
00000029 jmp 0000004B
0000004b nop
0000004c lea esp,[ebp-0Ch]
0000004f pop ebx
00000050 pop esi
00000051 pop edi
00000052 pop ebp
00000053 ret
000008ac pop eax
000008ad jmp eax

then the debugger stops.

Is the only solution to set the DLL unmanaged? Then I've to DllImport this DLL in the C# Project and the interaction of Dll<-->C# gets much complexer :?.

betajaen

02-07-2009 16:20:35

I'm old and set in my ways, so I don't have any C# experience apart from "Hello World" .net applications (which I have quickly forgotten) - but it seems unmanaged is the way to go.

Otherwise you could rewrite OGRE3DRenderSystem in C#. :?

andihit

02-07-2009 17:18:49

Now I 've tried the unmanaged DLL, and when I got it working with the C# program, it throws the same error.

Then I wondered, and made an small console application insted the DLL of the (unmanaged) C++ code.
And, guess what, stuck at the same line :(.

The error:
Exception at 0x01446003 in tuxRunnerPhysics.exe: 0xC0000005: access exception when read at position 0x00000068 (Original in german: "Unbehandelte Ausnahme bei 0x01446003 in tuxRunnerPhysics.exe: 0xC0000005: Zugriffsverletzung beim Lesen an Position 0x00000068.")


I thought, maybe VC++ had some error cause I created a DLL and then converted it to a console application, so I made a new project:
win32-console application (means no CLI --> unmanaged)
with lib dependencies: NxOgre_Debug.lib NxOgreOGRE3D_Debug.lib
source code: http://pastebin.com/m1e6de9c0
DLLs in Debug-Folder: Mogre_d.dll NxOgre_Debug.dll OgreMain_d.dll

But, same error. What can be wrong?

betajaen

02-07-2009 18:19:57

Intresting.

Are you sure its the same error, as I would figure it would be because the required Ogre classes haven't been created in that code at all.

[Edit]

Downloading Visual Studio 2008 C# Express, to see if I can replicate then fix the problem. I hear MOGRE is pretty good too :)

andihit

02-07-2009 18:43:12

Are you sure its the same error
yes

as I would figure it would be because the required Ogre classes haven't been created in that code at all.
Yeah, but in that code the Ogre classes aren't needed, and I want to create the Ogre things with MOGRE in the C# application.
Or does the OGRE3DRenderSystem need some static member of an Ogre-class, which should have been initialised by creating OGRE objects, before creating the new OGRE3DRenderSystem object?

Downloading Visual Studio 2008 C# Express, to see if I can replicate then fix the problem.
I hope I 'm not the only one with that error :wink:.

betajaen

02-07-2009 18:46:45

as I would figure it would be because the required Ogre classes haven't been created in that code at all.
Yeah, but in that code the Ogre classes aren't needed, and I want to create the Ogre things with MOGRE in the C# application.
Or does the OGRE3DRenderSystem need some static member of an Ogre-class, which should have been initialised by creating OGRE objects, before creating the new OGRE3DRenderSystem object?.

Yep. Ogre's Root, ResourceSystem, SceneManager and other classes are expected to be created and working when the OGRE3DRenderSystem is created.

If I get a fix working, I'll try and rewrite the Ogre3DRenderSystem in C# for MOGRE. I need to brush up on my C# skills. :)

[Edit]

Okay, Got it all installed. MOGRE 1.6.2 downloaded. Any chance you can lend me a test app or something to work from. Although C# look C++ like, I don't really have the time to learn it all and make a Cake-lite app. ;)

andihit

02-07-2009 20:46:59

Yep. Ogre's Root, ResourceSystem, SceneManager and other classes are expected to be created and working when the OGRE3DRenderSystem is created.
Oh.. didn't thought of that first. Bad singletons :roll:.

If I get a fix working, I'll try and rewrite the Ogre3DRenderSystem in C# for MOGRE.
I appreciate that :).

Any chance you can lend me a test app or something to work from.
Sure, I've extended the tutorial a bit with an plane at the ground and a box in the air (which should fall down with the physic engine :)).
If you want to debug, copy the needed DLLs & OGRE files in the bin/Debug directory (I've deleted the Debug-directory cause otherwise the zip would be so big).
http://files.andihit.net/testApp.zip

Although C# look C++ like, I don't really have the time to learn it all and make a Cake-lite app. ;)
C# is much easier than C++ (in my opinion), so it won't be difficult ;-) (and the IntelliSense of the C# Express is great).


A question:
When the OGRE3DRenderSystem is in C#, then I'll have to create the World and the Scene in C++-DLL, and the Bodies in C#? Or how should the C# MOGRE program <--> C++ NxOgre physics interaction be?

andihit

05-07-2009 14:21:11

I 've decided to write the 3D Stuff in C++ now and only the logic game stuff (or nothing? don't know jet ;-)) in C#.

So I can use NxOgre now :). And it works.. :).

betajaen

05-07-2009 14:42:19

Fair enough, C++ is better. )

I would like to create some NxOgre bindings into C# though.