2014-02-26 41 views
0

我在Objective C中有一個類來發現網絡打印機。我確實找到了我的打印機,但是當我的代碼稍後[在退出DiscoverDevices之後]檢查爲空/修剪設備列表時,它會拋出一個EXC_BAD_ACCESS code=2。我是Objective C新手,真的需要你的幫助。隨意編碼審查以及。NSCFString上的EXC_BAD_ACCESS代碼= 2

我懷疑PrinterCallBack_PUT_bluetoothDevices([jsonString description]); 一些問題字符串格式/地址,但不知道

代碼如下:

#import "DiscoverDevices.h"  
#import "ePOS-Print.h"  
#import "java_lang_String.h"; 
#import "org_json_me_JSONObject.h"; 

#define DISCOVERY_INTERVAL 0.5 

@interface DiscoverDevices() 
- (void)timerFindPrinter:(NSTimer*)timer; 
- (void)findStart;  
- (void) doNothing;  
@end  

@implementation DiscoverDevices  
@synthesize delegate = delegate_; 


- (id)init {  
    self = [super init];  
    if (self) {  
     printerList_ = [[NSMutableArray alloc] init];  
    }  
    [self findStart];  
    [self timerFindPrinter:timer_];  
    //TODO:find a better way to wait till printer is discovered here  
    // for now works  
    for (int i=0; i<500; i++) { 
     if (printerList_.count>0) { 
      break;  
     }  
     [self timerFindPrinter:timer_];  
    } 
    NSLog(@"-----printerList = %@",printerList_); 
    NSError * error;  
    NSMutableDictionary *dictObj = [[NSMutableDictionary alloc] init]; 
    for (int i=0; i<printerList_.count; i++) {  
     NSLog(@"printerList[%d] = %@",i,printerList_[i]);  
     [dictObj setObject:printerList_[i] forKey:printerList_[i]];  
     NSLog(@"jsonobject put [%d]",i);  
    }  
    NSLog(@"Dict : %@",dictObj);  
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictObj options:NSJSONWritingPrettyPrinted error:&error]; 
    if (!jsonData) {  
     NSLog(@"Got an error: %@",error);  
    } else {  
     NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];  
     jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\n" withString:@""];  
     jsonString = [jsonString stringByReplacingOccurrencesOfString:@" " withString:@""]; 
     NSLog(@"In native discoverDevices devices found jsonstring = %@, %s", jsonString, object_getClassName([jsonString class])); 

     if ([jsonString length]>2) {     
        // This is where we set the list of printers found. 
        com_nvsoft_s2pay_print_PrinterCallBack_PUT_bluetoothDevices([jsonString description]); 
        NSLog(@"In native discoverDevices written json to callback now reading...."); 
        NSLog(@"In native discoverDevices from cback json = %@, %s", com_nvsoft_s2pay_print_PrinterCallBack_GET_bluetoothDevices(),object_getClassName([com_nvsoft_s2pay_print_PrinterCallBack_GET_bluetoothDevices() class])); 
      }else{ 


      com_nvsoft_s2pay_print_PrinterCallBack_PUT_bluetoothDevices(nil);  
     } 

      com_nvsoft_s2pay_print_PrinterCallBack_PUT_result(1);  
      NSLog(@"In native discoverDevices completed jsondaata");  
     }  
     NSLog(@"In native discoverDevices exiting after jsondaata");  
     NSLog(@"In native discoverDevices from cback json = %@", com_nvsoft_s2pay_print_PrinterCallBack_GET_bluetoothDevices());  
     return self;  
    }  


- (void)dealloc  
{  
    [printerList_ release];  
    [super dealloc];  
} 
//find printer timer  
- (void)timerFindPrinter:(NSTimer*)timer  
{  
    NSLog(@"In timerFindPrinter");  
    int result = 0;  
    NSArray *items = [EpsonIoFinder getResult:&result]; 
    if(items != nil && result ==EPSONIO_OC_SUCCESS){  
     bool change = NO;  
     if([printerList_ count] != [items count]){  
      [printerList_ release];  
      printerList_ = [items retain];  
      change = YES;  
     }  
     /*if(change){  
     [self performSelectorOnMainThread:@selector(doNothing) withObject:nil waitUntilDone:YES];  
     }*/  
    }  
} 
    //find start/restart  
- (void)findStart  
{  
    NSLog(@"In findStart");  
    //stop old finder  
    [EpsonIoFinder stop];  
    //stop find timer  
    if(timer_ != nil){  
     [timer_ invalidate];  
     [timer_ release];  
     timer_ = nil; 
    } 
    //clear list 
    [printerList_ release];  
    printerList_ = [[NSMutableArray alloc] init];  
    //if(printerView_ != nil){  
    // [printerView_ reloadData];  
    //} 

    //find start 
     int result = [EpsonIoFinder start:EPSONIO_OC_DEVTYPE_TCP FindOption:@"255.255.255.255"];  
    if(result != EPSONIO_OC_SUCCESS){ 
      return ; 
     } 
    //start timer  
    timer_ = [NSTimer scheduledTimerWithTimeInterval:DISCOVERY_INTERVAL 
               target:self 
              selector:@selector(timerFindPrinter:) 
              userInfo:nil  
              repeats:YES];  
} 
@end 

我的控制檯輸出爲:

2014-02-26 11:50:24.661 S2PayApp[10749:1807] -----printerList = (

    "192.168.0.13" 

) 

2014-02-26 11:50:24.662 S2PayApp[10749:1807] printerList[0] = 192.168.0.13 

2014-02-26 11:50:24.662 S2PayApp[10749:1807] jsonobject put [0] 

2014-02-26 11:50:24.663 S2PayApp[10749:1807] Dict : { 

    "192.168.0.13" = "192.168.0.13"; 

} 

2014-02-26 11:50:24.664 S2PayApp[10749:1807] In native discoverDevices devices found jsonstring = {"192.168.0.13":"192.168.0.13"}, __NSCFString 

2014-02-26 11:50:24.664 S2PayApp[10749:1807] In native discoverDevices written json to callback now reading.... 

2014-02-26 11:50:24.665 S2PayApp[10749:1807] In native discoverDevices from cback json = {"192.168.0.13":"192.168.0.13"}, __NSCFString 

2014-02-26 11:50:24.665 S2PayApp[10749:1807] In native discoverDevices completed 
jsondaata 

2014-02-26 11:50:24.666 S2PayApp[10749:1807] In native discoverDevices exiting after jsondaata 

2014-02-26 11:50:24.667 S2PayApp[10749:1807] In native discoverDevices from cback json = {"192.168.0.13":"192.168.0.13"} 

2014-02-26 11:50:24.667 S2PayApp[10749:1807] --3-- In native discoverDevices from cback json = {"192.168.0.13":"192.168.0.13"} 

2014-02-26 11:50:24.668 S2PayApp[10749:1807] completed discoveing ntwk printers 

2014-02-26 11:50:24.669 S2PayApp[10749:1807] devices r9= {"192.168.0.13":"192.168.0.13"},__NSCFString 

2014-02-26 11:50:24.669 S2PayApp[10749:1807] devices r4= {"192.168.0.13":"192.168.0.13"},__NSCFString 
(lldb) 

這裏的確切類他的代碼被破壞:

JAVA_BOOLEAN com_nvsoft_s2pay_util_StringUtil_isEmpty___java_lang_String(JAVA_OBJECT n1) 
{ 
    if (!__TIB_com_nvsoft_s2pay_util_StringUtil.classInitialized) __INIT_com_nvsoft_s2pay_util_StringUtil(); 
    //XMLVM_BEGIN_WRAPPER[com_nvsoft_s2pay_util_StringUtil_isEmpty___java_lang_String] 
    XMLVM_ENTER_METHOD("com.nvsoft.s2pay.util.StringUtil", "isEmpty", "?") 
    XMLVMElem _r0; 
    XMLVMElem _r1; 
    XMLVMElem _r2; 
    _r2.o = n1; 
    XMLVM_SOURCE_POSITION("StringUtil.java", 152) 
    //NSLog(@"In StringUtil devices r2= %@", _r2.o); 
    if (_r2.o == JAVA_NULL) goto label22; 
    XMLVM_CHECK_NPE(2) 
    _r0.o = java_lang_String_trim__(_r2.o); 
    // "" 
    _r1.o = xmlvm_create_java_string_from_pool(13); 
    //NSLog(@"In StringUtil devices r0= %@,%s", _r0.o); 
    //NSLog(@"In StringUtil devices r1= %@,%s", _r1.o); 

    //java_lang_String_equals___java_lang_Object[1] 
    XMLVM_CHECK_NPE(0) 
//************ Code breaks here 
    _r0.i = (*(JAVA_BOOLEAN (*)(JAVA_OBJECT, JAVA_OBJECT)) ((java_lang_String*) _r0.o)->tib->vtable[1])(_r0.o, _r1.o); 
    if (_r0.i != 0) goto label22; 
    // "null" 
    _r0.o = xmlvm_create_java_string_from_pool(9); 
    //java_lang_String_equals___java_lang_Object[1] 
    XMLVM_CHECK_NPE(2) 
    _r0.i = (*(JAVA_BOOLEAN (*)(JAVA_OBJECT, JAVA_OBJECT)) ((java_lang_String*) _r2.o)->tib->vtable[1])(_r2.o, _r0.o); 
    if (_r0.i == 0) goto label24; 
    label22:; 
    _r0.i = 1; 
    label23:; 
    XMLVM_EXIT_METHOD() 
    return _r0.i; 
    label24:; 
    _r0.i = 0; 
    goto label23; 
    //XMLVM_END_WRAPPER 
} 
+0

你在哪一行得到exc_bad_access?在日誌輸出中我看不到它的任何痕跡... – sergio

+0

轉到產品 - >方案 - >編輯方案 - >診斷,選中標記'啓用殭屍對象',然後在此放置跟蹤。 – NeverHopeless

+0

啓用殭屍並沒有改變任何東西。相同的控制檯輸出和錯誤消息。我使用Codenameone生成的源代碼和iOS本機代碼。這裏的代碼可以工作,但是在退出這個類之後,它會失敗。在下面的評論中附上代碼。 – kpan

回答

0

當你看到那種崩潰的故障是在其他地方。我猜你在某處導致了這種內存損壞。通常這是由調用具有錯誤參數類型的c函數或通過保持對GC對象的引用或保留對已發佈Objective-C對象的引用來訪問無效內存引起的。

你可以在xcode的左邊看到崩潰時的調用堆棧,只需在那裏的模式之間切換,你應該看到你的所有線程都有一個很好的堆棧,可以讓你指出發生了什麼。它有點混亂,雖然主要的iOS線程有點遲鈍。

相關問題