2013-05-30 36 views
0

我使用FSCopyObjectAsync在Cocoa應用程序中複製文件。問題是,每當我嘗試設置info字段(類型爲void *的對象)時,應用程序因爲EXEC_BAD_ACCESS而崩潰。我不確定我做錯了什麼。爲什麼在設置FSFileOperationClientContext的信息指針時收到EXEC_BAD_ACCESS?

這裏是我的代碼:

// Start the async copy. 
FSFileOperationClientContext *clientContext = NULL; 
if (spinner != nil) { 
    clientContext->info = (__bridge void *)(spinner); // <- Problem here! 
} 

status = FSCopyObjectAsync(fileOp, 
          &source, 
          &destination, // Full path to destination dir. 
          CFSTR("boot.iso"), // Copy with the name boot.iso. 
          kFSFileOperationDefaultOptions, 
          copyStatusCallback, 
          0.5, // How often to fire our callback. 
          clientContext); // The progress bar that we want to use to update. 

CFRelease(fileOp); 

我使用ARC,如果我註釋掉處理clientContext的線條和在FSCopyObjectAsync的最後一個參數傳遞NULL它的工作原理,但嚴重削弱我的應用程序的功能。這絕對是任務,因此,這是造成這個問題的原因。

回答

1

您正在創建一個NULL指針而不分配它,然後嘗試引用它。更改代碼,以便將它分配給堆棧並傳遞其地址,如下所示。

// Start the async copy. 
FSFileOperationClientContext clientContext; 
if (spinner != nil) { 
    clientContext.info = (__bridge void *)(spinner); 
} 

status = FSCopyObjectAsync(fileOp, 
          &source, 
          &destination, // Full path to destination dir. 
          CFSTR("boot.iso"), // Copy with the name boot.iso. 
          kFSFileOperationDefaultOptions, 
          copyStatusCallback, 
          0.5, // How often to fire our callback. 
          &clientContext); // The progress bar that we want to use to update. 

CFRelease(fileOp); 
+0

因錯誤'error:address不包含指向對象文件中的節的節「而失敗。 – SevenBits

+0

不同的問題,很難說沒有看到完整的代碼。你看過使用FSCopyObjectAsync的各種示例代碼嗎? – parry

+0

是的,但他們不是很有幫助。我找不到任何正在做我正在做的事情的代碼。該代碼雖然在GitHub上是開源的,這裏是鏈接到有問題的文件:http://bit.ly/ZhzyCG有問題的代碼是一半以上。 – SevenBits

相關問題