2015-10-14 59 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 
}