简体   繁体   English

Unity应用程序中从Objective C到C#的回调释放内存中的内存问题

[英]Memory issue in releasing memory in Callback from Objective C to C# in Unity application

I need to pass char* from Objective C to C# in Unity application. 我需要在Unity应用程序中将char *从Objective C传递到C#。 The issue now is when char* is released using free(), the application crash. 现在的问题是使用free()释放char *时,应用程序崩溃了。

I have a callback function in Objective C side as 我在目标C端有一个回调函数

Unitybridge.mm

static UnityMultipleDevicesCallback MultipleDevicesLastCallBack = NULL;
void MultipleDevicesConnectCallback(UnityMultipleDevicesCallback callbackMethod)
{
    NSLog(@"callback is assigned");
    MultipleDevicesLastCallBack = callbackMethod;//a simple assignment using the connect method

}

void MultipleDevicesCallMethod(NSMutableArray *devices)
{
    NSLog(@"devices: %@", devices);
    if(MultipleDevicesLastCallBack != NULL){
        @autoreleasepool {
           devs_ = (char*)malloc([devices count]+1 * sizeof(char));

           for (NSString *s in devices) {

               if(strlen(devs_) > 0){
                   const char *d = "/";
                   strcat(devs_,d);
                   strcat(devs_, [s UTF8String] );
               }
               else{
                   strcpy(devs_, [s UTF8String]);
               }

           }           
        }

        MultipleDevicesLastCallBack(devs_);

    }
}
void Freedevs_()
{

    if(devs_!=nullptr){
        free(devs_);
        NSLog(@"devs_ is free now");
    }

}

At C# side, 在C#端,

     //
    [DllImport ("__Internal")]
    private static extern void Freedevs_();
    //Callback
    public delegate void UnityMultipleDevicesCallbackDelegate(string devices);
    [DllImport ("__Internal")]
    public static extern void MultipleDevicesConnectCallback(UnityMultipleDevicesCallbackDelegate callbackMethod);
    [MonoPInvokeCallback(typeof(UnityMultipleDevicesCallbackDelegate))]
    private static void MultipleDevicesCallback(string devices)
    {

        BLEdevices = (String)devices.Clone();
        Debug.Log ("Multiple devices callback now " + BLEdevices);
        //Freedevs_ ();
    }

I tried two options. 我尝试了两种选择。 One is @autoreleasepool and second one is without using @autoreleasepool , I call void Freedevs_() . 一个是@autoreleasepool ,第二个是不使用@autoreleasepool ,我称之为void Freedevs_()

Using @autoreleasepool , not sure sometimes it works and sometimes the application crash. 使用@autoreleasepool ,不确定有时是否@autoreleasepool ,有时应用程序崩溃。 If I call Freedevs_ (), definitely the application crash. 如果我调用Freedevs_(),肯定会导致应用程序崩溃。

What could be the solution for this? 对此有什么解决方案?

I am fortunate that I found this article here . 我很幸运在这里找到了这篇文章。

According the article, I need to use [MarshalAs(UnmanagedType.LPStr)] at my C# side and updated. 根据文章,我需要在C#端使用[MarshalAs(UnmanagedType.LPStr)]并进行更新。 Please note that I don't use malloc anymore and that is sort of not reliable thing in such usage . 请注意, I don't use malloc anymore and that is sort of not reliable thing in such usage

    Objectiv C
    void MultipleDevicesCallMethod(NSMutableArray *devices)
    {


      if(MultipleDevicesLastCallBack != NULL){
           NSString *d = @"";
           int cnt = 0;
           for (NSString *s in devices) {
               d = [d stringByAppendingString:s];
               if(cnt < [devices count]-1 &&  [devices count] > 1 ){
                   d = [d stringByAppendingString:@"/"];
                   cnt++;
               }

           }
           const char* devs_ =  [d cStringUsingEncoding:NSUTF8StringEncoding];
           MultipleDevicesLastCallBack(devs_);


      }       

    }

     C#
     public delegate void UnityMultipleDevicesCallbackDelegate(string devices);
    [DllImport ("__Internal", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
    public static extern void MultipleDevicesConnectCallback(UnityMultipleDevicesCallbackDelegate callbackMethod);
    [MonoPInvokeCallback(typeof(UnityMultipleDevicesCallbackDelegate))]
    private static void MultipleDevicesCallback([MarshalAs(UnmanagedType.LPStr)]string devices)
    {

        BLEdevices = (String)devices.Clone();
        Debug.Log ("Multiple devices callback now " + BLEdevices);
        Freedevs_ ();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM