【问题标题】:Xcode Segmentation Fault 11 (custom url)Xcode Segmentation Fault 11(自定义网址)
【发布时间】:2015-10-14 09:18:23
【问题描述】:

项目概述: 我目前正在使用 Unity iOS 并在我正在使用 customURL 的 2 个应用程序之间发送信息。 App A 成功向 Ap​​p B 发送数据,App B 将接收到的数据存储在静态变量中。但是,当应用程序完成启动并尝试检索数据(NSString*)时,会发生分段错误 11(应用程序崩溃)。

Xcode 新手,我对解决分段错误一无所知。请指导我解决问题。谢谢。

CustomUrlContoller.mm

#import "CustomURLController.h"
#import <stdio.h>
#import <stdlib.h>

@implementation CustomURLUnity
static int AccountID = -1;
static NSString *AccountIgn = @"";
+(void)SetAccount:(int)NewID AccIgn:(NSString*)NewIgn
{
    AccountID = NewID;
    AccountIgn = NewIgn;
}

+(int)GetAccountID { return AccountID; }
+(NSString*)GetAccountIgn { return AccountIgn; }
@end

extern "C" {
    int GetAccID() { return [CustomURLUnity GetAccountID]; }  //WORKS

    char* GetAccIGN() {
        NSLog(@"GetAccIGN: %@", [CustomURLUnity GetAccountIgn]);  //CRASH
        NSString* IgnString = [CustomURLUnity GetAccountIgn];
        if(IgnString isEqual:[NSNull null]])
        {
             IgnString = @"";
        }
        const char* stringAsChar = [IgnString UTF8String];
        char* cpy = (char*)calloc(1, [IgnString length]+1);
        strncpy(cpy, stringAsChar, [IgnString  length]);
        return cpy;
    }
}

UnityAppController.mm

#import "CustomURLController.h"
-(BOOL) application:(UIApplication*)application openURL:(NSURL*)url
        sourceApplication:(NSString*)sourceApplication
        annotation(id)annotation
{
    /*...default...*/
    if([sourceApplication isEqualToString:@"com.xxx.xxx"])
    {
        NSString* QueryStrings = [url query];
        NSArray* AccountStrings = [QueryStrings componentsSeparatedByString: @"&"];
        NSString* AccountID = [AccountStrings objectAtIndex:0];
        NSString* IGN = [AccountStrings objectAtIndex:1];
        int AccountIDNo = [AccountID intvalue];
        [CustomURLUnity SetAccount:AccountIDNo AccIgn:IGN];
        NSLog(@"openURL GetIgn: %@", [CustomURLUnity GetAccountIgn]);            
    }
    return YES;
}

openURL 中的 NSLog 根据第一个 App 的输入正确记录 Ign。但是,当 App B 完成启动并尝试通过调用 extern 方法“GetAccIGN”获取 Ign 时,App B 崩溃并出现分段错误 11。

外部方法“GetAccID”根据输入成功返回AccountID。

【问题讨论】:

    标签: ios xcode unity3d nsstring segmentation-fault


    【解决方案1】:

    最后,找到了解决我的问题的方法。 似乎 NSString* 在调用 openURL 后被取消引用。 因此,当我尝试调用 NSString* 时,指针未指向任何导致分段错误的内存位置。

    为了解决分段错误,我必须在将 NSString 设置为变量时对其进行复制。

    +(void)SetAccount:(int)NewID AccIgn:(NSString*)NewIgn
    {
        AccountID = NewID;
        [AccountIgn release];  //Release the previous memory allocation
        AccountIgn = [NewIgn copy];  //Make a copy of the new string and assign
    }
    

    【讨论】:

      猜你喜欢
      • 2019-03-26
      • 2014-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-17
      • 1970-01-01
      • 2013-02-15
      • 1970-01-01
      相关资源
      最近更新 更多