SOLVED: ObjectiveC fails with pointer being freed was

Hi I am trying to figure out a problem I am having with calling a simple Objective-C method from Unity iPhone Advanced.

My simple test case before getting into anything too complex was to pass a string to my ObjC method, log that string and then return another string to Unity via UnitySendMessage. Depending on what I tried I was getting this error.

matchPoint(674,0x3e2fe7c8) malloc: *** error for object 0xbab500: pointer being freed was not allocated

To simplify things I built a new project, implemented the same C# code and ObjC method code and it works as expected. So back to my real project and I can’t seem to shake this error.

In fact I commented out all the code in the method and I still get the error, even after doing a Build/Run from Unity and Build Clean from Xcode.

I am looking for any suggestions on how to debug this issue.

I am using Unity IPA 1.7 and iOS SDK 4.0

Any suggestions would be appreciated.

iByte

1 Like

strings sent to the objc side must first be converted to nsstring
strings send back must go through the cocoaToMonoString (I think thats the exact naming) function to make them appropriate strings for anything.

the only place you can use NSStrings is for NSUserDefaults ↔ PlayerPrefs

1 Like

dreamora, thanks for the reply - Here is a code sample of what works

C#

using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;

public class GameLogic : MonoBehaviour {

	
	[DllImport ("__Internal")]
    private static extern void _MyObjectiveCMethod ( string myString );
	
	public GameObject gameText;
	
	// Use this for initialization
	void Start ()
	{
	
    string message = "myMessage";
    _MyObjectiveCMethod (message);

		
	}
	
	// Update is called once per frame
	void Update ()
	{
	
	}
	
	
	
	void UnityCallBack (string MyData)
	{
		TextMesh gameTemp;
		gameTemp = gameText.GetComponent(typeof(TextMesh)) as TextMesh;
		gameTemp.text = MyData;
		//Debug.Log(MyData);
	}
	
	
}

Object-C Code - added to AppController.mm

extern "C" {    
    void _MyObjectiveCMethod ( const char *myData ) {
        NSLog( [NSString stringWithUTF8String: myData ? myData : ""]);
	UnitySendMessage("GameObject", "UnityCallBack", "This works!");
		
	}   
}

I see myMessage in the console and I see “This Works!” on my iPhone.

What I am struggling with is that when i completely comment out the code in the objective-c method in my real application, I still get this error.

:frowning:

1 Like

the “path to c” looks right (its no objc you have there, but really c that calls objc)

can’t comment on the opposite direction though as I haven’t used that path yet, but I know that OpenFeint uses it in their unity package so that might potentially be a starting point.

There is a Plugin Tutorial from Unity (somewhat hard to find weithin the docs. Something with bonjour) have a look at that. Their Xcode Part is a Little different than Yours

Hi Marjan, thanks for responding, I have looked at that sample as well as Unity Forum postings and Unity Answers.

What has me stumped is that a function call with all code commented out like this still causes a malloc error.

extern "C"
{

	void makeAtlasObjC (const char *gameType)
	{
		// Placeholder for code
	}
	
}

hmm … sounds ugly … potentially some os4 thing? I’ve not seen malloc errors but I’ve not tested my stuff with the live sdk, only OS4 GM

Okay - it was my fault :slight_smile: but talking about it helped me notice my mistake.

in my real game I had this as the declaration

[DllImport (“__Internal”)]
private static extern string makeAtlasObjC (string gameType);

when it should have been

[DllImport (“__Internal”)]
private static extern void makeAtlasObjC (string gameType);

to match the actual function.

I had originally intended to return a string but decided I wanted to use UnitySendMessage instead.

Thanks dreamora and marjan.

iByte

1 Like

hehe, epic :wink:
but glad you got it solved

I had hard time finding informations about callback from ObjectiveC code. That thread helped me out, but I wrote another thread with the problem I encountered, hope it could help someone.

Realy epic! I had the same problem :slight_smile: and exact same dummy error! Thank you for posting this! You’ve made my day! By the way, I don’t know why - but at Xcode8 build worked just fine:eyes:…