尝试编写Unity iOS插件时出现Malloc错误

tho*_*xey 4 objective-c unity-game-engine ios

尝试编写一个快速的Unity插件以从剪贴板中获取数据时,有问题的函数如下:

extern "C"
    {
        const char * _importString()
        {
            UIPasteboard *result = [UIPasteboard generalPasteboard];
            NSString * resultString = [result string];
            return [resultString UTF8String];
        }
    }
Run Code Online (Sandbox Code Playgroud)

但是,当我激活该功能时,它给了我以下错误:

GfxDevice: creating device client; threaded=1
Initializing Metal device caps: Apple A9 GPU
Initialize engine version: 2017.1.0f3 (472613c02cf7)
2017-08-26 13:46:21.395230+0100 ProductName[8608:2930827] [Common] _BSMachError: port 5e03; (os/kern) invalid capability (0x14) "Unable to insert COPY_SEND"
2017-08-26 13:46:21.400889+0100 ProductName[8608:2930827] [Common] _BSMachError: port 5e03; (os/kern) invalid capability (0x14) "Unable to insert COPY_SEND"
UnloadTime: 4.034333 ms
ProductName(8608,0x1b8100b40) malloc: *** error for object 0x1740a6191: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Run Code Online (Sandbox Code Playgroud)

主要使用Unity C Sharp进行编写,因此对iOS插件了解甚少,也不知道从哪里开始,任何帮助都值得赞赏。

编辑:

对于那些在家玩的人,这里是相关的Unity片段:

[DllImport("__Internal")]
private static extern string _importString();

//retrieves the clipboard contents from xcode
public string iPhoneImportClipboard()
{
    return _importString();
}
Run Code Online (Sandbox Code Playgroud)

bbu*_*bum 6

的结果-string不得视为malloc指针。

您需要先返回malloc一个缓冲区并strcpy说一个字符串。


extern "C"
    {
        const char * _importString()
        {
            UIPasteboard *result = [UIPasteboard generalPasteboard];
            NSString * resultString = [result string];
            return strdup([resultString UTF8String]);
        }
    }
Run Code Online (Sandbox Code Playgroud)