Allocating and initializing object in one function, sending it to other and releasing it there works?
// ViewController.m - implements downloader's protocol downloadComplete
- (void) startDownload
{
Downloader *downloader = [[downloader alloc] init];
[downloader setDelegate:self];
[downloader startDownloading];
// [downloader release] or autorelase makes the program crash
}
- (void) downloadComplete: (id) downloadedContent
{
[downloadedContent release]; // will this release the object allocated in the first function? Or do I set [self release] in dealloc of Downloader.m? Or any other way to do it?
}
// Downloader.m
开发者_如何转开发
- (void) startDownloading
{
// download some data
[[self delegate] downloadComplete:self];
}
For what I see, - (void) downloadComplete: (id) downloadedContent
is a delegate method. It's a good habit to release only the objects you own (except where explicitly said) and the delegate do no own that object. It's far better and clearer to release the object in dealloc
method of ViewController.m
Actually it's not very cocoa-ish. I'd suggest creating downloader
as an instance method, allocating it when needed and releasing it in dealloc. If you can use download functionality several times in same controller instance - make sure to release and nil-out downloader object before creating it.
精彩评论