以下代碼用於下載圖像並返回塊的結果,以便利用塊和Grand Central Dispatch的異步功能。我發現如果圖像或錯誤對象是零,我會得到一個EXC_BAD_ACCESS錯誤。如果參數的值爲零,是否會導致錯誤?Objective-C中的塊是否可以將一個零值作爲參數?
失敗的部分是returnImage塊,用於在方法結束時返回圖像。
- (void)downloadImageWithBlock:(void (^)(UIImage *image, NSError *error))returnImage {
dispatch_queue_t callerQueue = dispatch_get_current_queue();
dispatch_queue_t downloadQueue = dispatch_queue_create("Image Downloader Queue", NULL);
dispatch_async(downloadQueue, ^{
UIImage *image = nil;
NSError *error;
// get the UIImage using imageURL (ivar)
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"Downloading: %@", imageURL);
});
NSURL *url = [NSURL URLWithString:imageURL];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSURLResponse *urlResponse = nil;
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
if (!error && response) {
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"Success!");
});
image = [UIImage imageWithData:response];
}
// image will be nil if the request failed
dispatch_async(callerQueue, ^{
returnImage(image, error);
});
});
dispatch_release(downloadQueue);
}
下面是我不知道如何閱讀的堆棧跟蹤。
0x00002d20 <+0000> push %ebp
0x00002d21 <+0001> mov %esp,%ebp
0x00002d23 <+0003> sub $0x18,%esp
0x00002d26 <+0006> mov 0x8(%ebp),%eax
0x00002d29 <+0009> mov %eax,-0x4(%ebp)
0x00002d2c <+0012> mov -0x4(%ebp),%eax
0x00002d2f <+0015> mov 0x14(%eax),%eax
0x00002d32 <+0018> mov %eax,(%esp)
0x00002d35 <+0021> movl $0x3,0x4(%esp)
0x00002d3d <+0029> call 0x314c <dyld_stub__Block_object_dispose>
0x00002d42 <+0034> mov -0x4(%ebp),%eax
0x00002d45 <+0037> mov 0x18(%eax),%eax
0x00002d48 <+0040> mov %eax,(%esp)
0x00002d4b <+0043> movl $0x3,0x4(%esp)
0x00002d53 <+0051> call 0x314c <dyld_stub__Block_object_dispose>
0x00002d58 <+0056> mov -0x4(%ebp),%eax
0x00002d5b <+0059> mov 0x1c(%eax),%eax
0x00002d5e <+0062> mov %eax,(%esp)
0x00002d61 <+0065> movl $0x7,0x4(%esp)
0x00002d69 <+0073> call 0x314c <dyld_stub__Block_object_dispose>
0x00002d6e <+0078> add $0x18,%esp
0x00002d71 <+0081> pop %ebp
0x00002d72 <+0082> ret
0x00002d73 <+0083> nopw 0x0(%eax,%eax,1)
0x00002d79 <+0089> nopl 0x0(%eax)
什麼是使這個代碼難以調試的事實是,錯誤不會顯示在一行代碼中,所以我必須註釋掉大塊代碼以嘗試識別錯誤發生的位置。指出錯誤的代碼行很困難。 – Brennan 2011-05-23 23:58:35