• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
CAPE

Pointers invalid between C++ and C#.

4 posts in this topic

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

0

Share this post


Link to post
Share on other sites

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.

1

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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!

0

Share this post


Link to post
Share on other sites

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  
Followers 0