Pointers invalid between C++ and C#.

Started by
3 comments, last by sensate 11 years, 2 months ago

I'm experience a bit of a small problem. I have implemented a render system with a dll in C++, where the user injects data (model/view/projection matricse, colors, meshes and so on) through void pointers. It is quite handy, as the graphic API's usually don't care about the data type, just the structure and size of the data.

I have been using it as a part of a game that we are developing, and it works out quite good. But now I am in the mood of developing a editor to the render system in C#, and am quite stumped.

When i link an XNA matrix as a pointer to the render dll, the pointer stays intact in teh transfer to the dll, where it is kept for future references when updated in the editor. But, the data that the pointer points at, is not. Here is some code:

Editor (C#):


unsafe
{
[...]
                Matrix unsafeWorldMatrix = worldMatrix;
                void* worldMatrixPointer = (void*)(&unsafeWorldMatrix.M11);

                mRenderInterface.AddData("world", worldMatrixPointer);
[...]
}

While the pointer is in the editor code, the world matrix pointer is able to convert back to the matrix in question, with the data intact.


               // The data is intact
                Matrix worldMatrix = *(Matrix*)worldMatrixPointer;

But as soon as I send it to the render system, using the dll, it looses the data. The pointer is the same, but teh first value just points to 0, and the rest is invalid.


       // Sending the data to the render system
       // The data is still intact here, and lets say that the pointer value is 0x0c52af32
        [DllImport(DllName, SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
        static extern unsafe void SetSharedData(System.String dataID, void* data);
        public unsafe void AddData(String id, void* dataPtr)
        {
                SetSharedData(id, dataPtr);
        }

In the dll (c++)




        // Here the data is incorrect
        // the pointer is the same as before, but with CAPS letter (should not matter though)(0x0C52AF32)
        void hs::renderer::SetSharedData(const char* id, void* data)
        {
	        AssertFunctionAndThrow(DirectXManager::GetInstance()->GetContent()->AddSharedData(id, data));
        }

Both systems are compiled using x86 / Win32 settings, and both are in debug.

What is the deal here? I am aware that it is lack of knowledge, as I´m very unfamiliar with the correct procedures here, but as far as i know, the data should not change, as long as the pointer is intact...

BTW, when returning back from saving the pointer in the system, returning to the C# editor, the data in the pointer is intact.... WTF?

So, anyone who can point out my error? And as the rendersystem works as intended in the actual game, I know it is nothing wrong in the dll. And, teh only thing I do is storing the pointer in a map with a string identifier, so no interaction with hte actual pointer,

Any ideas are welcome.

// CAPE

David Gustavsson
Developer on "Hello space"
IndieDB
Advertisement

It sounds like your program and the DLL its calling have different memory heaps. A pointer returned by allocating memory in your editor heap will be invalid when you try to use it to locate data in your C++ DLL, but it will look perfectly valid when debugging from the editor.

I don't know anything about C# so I can't point you anywhere specific, but usually sharing allocated objects across processes is done with shared memory segments or memory mapped files.

Your pointer points to a local variable on the stack. As soon as your function ends on the managed side, the memory is popped off the stack and is no longer valid. Any pointers to it will be pointing to invalid memory.

Having native store a pointer to anything allocated on the managed side is very difficult to achieve. If your object is a local, the memory disappears when it goes out of scope. If it's allocated on the heap, the memory can be moved around by the garbage collector. My advice is to make a copy of the matrix and force updates when necessary.

Mike Popoloski | Journal | SlimDX

Oh shit, there my education went out the window. You think you are learnig something, then just: "MINDBLOWN. You dont know anyting. Study harder, douchbag.", or something like that.

Well, seems like it will be back to the drawing board. But, as it is an editor, i guess I can have it not som optimized, i guess... (shiver through my body).

But dudes, thanks for the quick reply. Very helpful.

David Gustavsson
Developer on "Hello space"
IndieDB

1.) Regarding your managed memory access, you should pin the data that you are working with before copying to the DLL.


using System.Runtime.InteropServices;

GCHandle pinnedWorldMatrix = GCHandle.Alloc(worldMatrix, GCHandleType.Pinned);
// Copy the the pinned memory to some local cache in "AddData".
_renderInterface.AddData("world", pinnedWorldMatrix.AddrOfPinnedObject());
pinnedWorldMatrix.Free();

I usually only use this interface on arrays of primitive data.

Your interface would be safer if you copied the data to a float[] locally.


float[]  floatArray = new float[16];

// I'm not sure what the XNA matrix operators look like, but
// for brevity we are just going to copy the data from your matrix to the floatArray.
for (int i = 0;i < 16; i++)
  floatArray[i] = worldMatrix[i];

GCHandle pinnedFloatArray = GCHandle.Alloc(floatArray, GCHandleType.Pinned);
// Copy the the pinned memory to some local cache in "AddData".
_renderInterface.AddData("world", pinnedFloatArray.AddrOfPinnedObject());
pinnedFloatArray.Free();

2.) I would seriously consider wrapping your unmanaged code up using something like SWIG or C++.NET to present a more concrete interface to your XNA layer.

Your life will also be made easier if you use a custom allocator in your c++ dll. With the number of boundaries you are crossing, it's always a good idea to be completely clear on where allocation and deallocation are occuring. Steve Streeting had some nice notes on this:

http://www.stevestreeting.com/2008/06/28/memory-man/

3.) Part of this notes assumes that you would be sending the data using an intptr_t, and using swig to add type maps for IntPtr to intptr_t.

I.e.


%typemap(ctype)  intptr_t "intptr_t"
%typemap(imtype) intptr_t "IntPtr"
%typemap(cstype) intptr_t "IntPtr"
%typemap(csin)   intptr_t "$csinput"
%typemap(in)     intptr_t %{ $1 = $input; %}
%typemap(out)    intptr_t %{ $result = $1; %}
%typemap(csout)  intptr_t { return $imcall; }

Bon Chance!

This topic is closed to new replies.

Advertisement