- Home /
Objective-C Plugin issue
I am using the Unity Plugin feature to transfer data from Obj-C to Unity. After much blind falling about, I eventually settled on the method outlined below.
Obj-C side:
char* MakeStringCopy (const char* string) { if (string == NULL) return NULL; char* res = (char*)malloc(strlen(string) + 1); strcpy(res, string); return res; }
struct PuzzleEngineData { const char* puzzleData; bool canRatePuzzle; int usedHints; };
extern "C" { void _LoadPuzzleData ( struct PuzzleData *puzzleData ) { // of course, these values actually come from data // stored elsewhere, but the gist is the same puzzleData->puzzleData = MakeStringCopy([@"puzzle string" UTF8String]); puzzleData->canRatePuzzle = YES; puzzleData->usedHints = 0; } }
C# side:
public struct PuzzleData { public string puzzleData; public bool canRatePuzzle; public int usedHints; }
[DllImport ("__Internal")] private static extern void _LoadPuzzleData ( ref PuzzleData puzzleData );
public static PuzzleData LoadPuzzleData () { PuzzleData data = new PuzzleData(); if ( Application.platform == RuntimePlatform.IPhonePlayer ) { LoadPuzzleData( ref data ); } return data; }
It all works - The data gets into Unity just fine. Right after the C method finishes executing, however, I am getting the following output in the console when running the app on-device in Debug mode:
PuzzleGame(2940,0x383f72d8) malloc: *** error for object 0xd35510:
Non-aligned pointer being freed (2)
*** set a breakpoint in malloc_error_break to debug
PuzzleGame(2940,0x383f72d8) malloc: *** error for object 0x62727a0:
Non-aligned pointer being freed (2)
*** set a breakpoint in malloc_error_break to debug
PuzzleGame(2940,0x383f72d8) malloc: *** error for object 0xd355b0:
Non-aligned pointer being freed (2)
*** set a breakpoint in malloc_error_break to debug
Research tells me that this error comes up when free() is called on something that hasn't been malloc'ed. I haven't the faintest clue about how to go about using malloc correctly to solve this problem in my particular instance; all the strings are malloc'ed, and the struct is created C# side.
Help? :)
===== EDIT =====
Turns out that making the struct's strings non const and actually using MakeStringCopy() for every instance of a string fixes it! Part misunderstanding and part pebcak (problem-exists-between-keyboard-and-chair)!
Thanks jonas echterhoff for taking the time to help me.
Answer by jonas-echterhoff · Dec 07, 2009 at 10:57 AM
While I'm not sure what exactly is happening here, I'm pretty sure you cannot simply cast a C++ string to a C# string in a struct, as they are represented different internally. However, IIRC, you should be able to do that by passing the C++ string as a return value. This is because the mono runtime detects the case of having strings as a return value and performs an automatic cast from C++ to C# strings in that case. At least that is how I remember it, probably worth experimenting with anyways.
Your answer
![](https://koobas.hobune.stream/wayback/20220613175943im_/https://answers.unity.com/themes/thub/images/avi.jpg)