2015-10-14 84 views
0

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

新的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; 
} 

的的NSLog中的OpenURL记录该IGN正确地根据从所述第一应用程序的输入。然而,当应用B完成启动并试图通过调用extern方法“GetAccign”来获得Ign时,应用B与分段错误11一起崩溃。

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

回答

0

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

要解决分段错误,我必须在将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 
}