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

标签 ios objective-c unity3d

尝试编写一个快速的 Unity 插件以从剪贴板中获取数据,所讨论的函数如下:

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

然而,当我激活该功能时,出现以下错误:

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

主要使用 Unity C sharp 编写,所以对 iOS 插件知之甚少,也不知道从哪里开始,感谢任何帮助。

编辑:

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

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

//retrieves the clipboard contents from xcode
public string iPhoneImportClipboard()
{
    return _importString();
}

最佳答案

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

您需要malloc 缓冲区和strcpy 在返回之前表示字符串。


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

关于ios - 尝试编写 Unity iOS 插件时出现 Malloc 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45895732/

相关文章:

objective-c - 在 Objective-C 中调用动态类的方法?

javascript - 为什么在Unity中仅使声音静音如此困难?

c# - Perlin 地形页面不匹配 : elusive bug

ios - 点击时如何对子类 UIView 执行操作?

ios - Xcode SpriteKit如何在不同屏幕尺寸的GameScene中适应标签?

ios - 将 CLLocationCoordinate2D 放入 Swift 的 CLLocation

unity3d - 如何导入 3D 场景(.obj 文件和 .mtl 文件)

ios - 接收未声明的私有(private)属性(property)选择器

objective-c - didStartMonitoringForRegion 一次调用两次 startMonitoringForRegion

ios - 在带有圆角的UIButton中添加底部边框