x


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.

more ▼

asked Dec 07 '09 at 07:29 AM

Robert gravatar image

Robert
117 3 2 8

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Dec 07 '09 at 10:57 AM

jonas echterhoff gravatar image

jonas echterhoff ♦♦
9.8k 7 23 104

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2013
x407

asked: Dec 07 '09 at 07:29 AM

Seen: 4126 times

Last Updated: Feb 25 '10 at 09:53 AM