Sign in to follow this  

MDX Sprite Class issue

This topic is 4200 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to create a MDX Sprite object using the constructor that takes an IntPtr to an unmanaged ID3DXSprite object. I have already setup the code to do this and it works for everything else I do (I do this for the Device and Textures and it works just fine). However, if I do this for the sprite my program crashes. I even tried creating a sprite using the constructor that takes a Device reference (my Device was also constructed with an IntPtr to an unmanaged Device) and this crashes as well. Is there some inherent problem with MDX sprites and unmanaged objects or am I just doing something incorrectly? Has anyone else run into this issue? Thanks,

Share this post


Link to post
Share on other sites
Curious, what is it you're doing with the IntPtr?

Reason for my question is that I run MDX and don't use IntPtr for anything.

Also, please post you're constructor implementation for the Sprite class. Explain any arguments passed to it.

-Devin

Share this post


Link to post
Share on other sites
The constructor I'm using to initialize my sprite object is explained in the following link

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_m/DirectX/ref/ns/microsoft.directx.direct3d/c/sprite/m/ctor1.asp

Here is a snippet of what I am doing:


public class MDX : MarshalByRefObject, ISerializable
{
public Device d3d;
public Sprite d2d;

unsafe public MDX()
{
d3d = new Device((IntPtr)MDirect3D.GetDirect3DDevice());
d2d = new Sprite((IntPtr)MDirect3D.GetD3DXSprite());
}
}


MDirect3D is a Managed C++ class that has several functions for getting unmanaged Direct3D resources from C++ land. Hence, GetDirect3DDevice() returns pointer to an unmanaged Device and similary GetD3DXSprite() returns a pointer to an unmanaged Sprite. Ultimately a CSharp object would make use of the d2d variable; however, the sprite would also be shared with C++ code that also makes use of it. The problem arises when I attempt to use the sprite in C++; the program will crash with the following error:

An unhandled exception of type 'System.AccessViolationException' occurred
in Unknown Module.

Additional information: Attempted to read or write protected memory. This
is often an indication that other memory is corrupt.

If I just use it in C# however then the program doesn't crash. I don't have a problem with other resources that I use in this fashion (such as the Device and textures). It only seems to be the sprite class.

I also tried doing the following:

unsafe public MDX()
{
d3d = new Device((IntPtr)MDirect3D.GetDirect3DDevice());
d2d = new Sprite(d3d);
}

and this crashed even worse than the first way (this just crahses on startup unlike the first way) with the following error:

Unhandled exception at 0x0187b70e in muppets.exe: 0xC0000005: Access
violation reading location 0x00000000.

My guess is the Sprite class has some kind of error in it because all other resources I use work fine with this method.

Share this post


Link to post
Share on other sites
Try enabling D3D debugging through the DirectX control panel (and enabling Unmanaged debugging for your C# project). Maybe the debug output might tell you more. Also posting the code for your MDirect3D class would tell us more what's happening there, as the error seems to come from the unmanaged part of your app.

Exactly what are you trying to accomplish with this code by the way? You can just create devices and sprites in pure C# without the need to use Managed C++ wrappers.

Share this post


Link to post
Share on other sites
This is basically part of a larger scale project we've been working on for the past year called MUPPETS (http://muppets.rit.edu). We're basically writing a large scale application that interfaces unmanaged C++ with C# and uses a DirectX render pipeline (hence the interfacing of unmanaged and managed DirectX). The core rendering is all done in unmanaged C++; however, some of it is also done in C#. Here is a snippet of the MDirect3D class:

static LPDIRECT3DDEVICE9 deviceHandle = NULL;
static LPD3DXSPRITE spriteHandle = NULL;
namespace ManagedBridge
{
public __gc class MDirect3D
{
public:
inline static void SetDirect3DDevice(void * device)
{
deviceHandle = (LPDIRECT3DDEVICE9)device;
}

inline static void * GetDirect3DDevice()
{
return deviceHandle;
}

inline static void SetD3DXSprite( void * sprite )
{
spriteHandle = (LPD3DXSPRITE)sprite;
}

inline static void * GetD3DXSprite()
{
return spriteHandle;
}
};
}

The SetD3DXSprite() and SetDirect3DDevice() are called by unmanaged C++. Managed Bridge is ultimately a module for interfacing C# and C++ and it gets generated as a DLL. All CSharp code that makes use of the ManagedBridge (and ultimately references the unmanaged resources) lies in a different Application Domain; this is the reason why the Setter functions exist in the first place. I know we've got a fairly messy/complex structure going at the moment so I'm trying my best to explain the relevant parts. We've spent a great deal of time working with this and doing lots of research on forums/websites and most of what we have is necessary despite being a bit on the messy side. Just note, the code above is working fine for the Device but not for the sprite.

Share this post


Link to post
Share on other sites

This topic is 4200 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this