我寫了一個函數來返回一個字符串到一個目錄路徑。我得到一個EXC_BAD_ACCESS,我認爲這個函數是原因。我需要保留nsstring什麼的?分配NSString時得到一個EXC_BAD_ACCESS
-(void) getRemoteFiles:(NSMutableArray *) M
{
[self createFileToAppDirectory];
if (!networkqueue) {
networkqueue:[[[ASINetworkQueue alloc] init] autorelease];
}
[[self networkQueue] cancelAllOperations];
[self setNetworkQueue:[ASINetworkQueue queue]];
[[self networkQueue] setDelegate:self];
[[self networkQueue] setRequestDidFinishSelector:@selector(requestFinished:)];
[[self networkQueue] setRequestDidFailSelector:@selector(requestFailed:)];
[[self networkQueue] setQueueDidFinishSelector:@selector(queueFinished:)];
int i;
for (i=0; i<[M count]; i++) {
NSString *url=[M objectAtIndex:i];
NSString* theFileName = [url lastPathComponent];
NSString *safestring=[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
if ([theFileName isEqualToString:@"nothing"]==NO) {
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:safestring]];
//think this is causing the problem
NSString *savepath=[self getDirectoryPathForFileName:theFileName];
//[request setDownloadDestinationPath:savepath];
[[self networkQueue] addOperation:request];
}
}
[[self networkQueue] go];
//error thrown after this point
}
-(NSString *)getDirectoryPathForFileName:(NSString *)filename
{
NSFileManager *filemgr;
NSArray *dirPaths;
NSString *docsDir;
NSString *newDir;
BOOL isDir;
filemgr =[NSFileManager defaultManager];
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
newDir = [docsDir stringByAppendingPathComponent:@"remix_data"];
if ([filemgr fileExistsAtPath:newDir isDirectory:&isDir]==NO) {
NSLog(@"dir should exist but does not... go funt dat");
}
NSString *localFilePath = [newDir stringByAppendingPathComponent:filename];
[filemgr release];
[docsDir release];
[newDir release];
return localFilePath;
}
你沒有對filemgr,docsDir和newDir執行alloc/init。那麼你爲什麼要釋放它們? – 2011-03-02 10:26:34
我還在學習內存管理,代碼來自一個例子。我猜這個例子是錯的? – dubbeat 2011-03-02 10:28:17