目标 C:如何在方法具有返回类型的方法中或在具有任何返回类型的覆盖方法中设置 NSAutoreleasePool?
Posted
技术标签:
【中文标题】目标 C:如何在方法具有返回类型的方法中或在具有任何返回类型的覆盖方法中设置 NSAutoreleasePool?【英文标题】:Objective C: how to set up a NSAutoreleasePool within a method where the method have a return type or within a overridden method with any return type? 【发布时间】:2011-07-05 11:55:48 【问题描述】:如何在方法具有返回类型的方法中设置 NSAutoreleasePool? 有没有办法做到这一点? 喜欢下面的方法:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation, AddressAnnotation>) annotation;
或者在一个被覆盖的方法中,比如:
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated;
我可以在 main.m 文件中看到如下:
int main(int argc, char *argv[])
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
//Do anything here
int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool release];
return retVal;
所以应该是这样的?
【问题讨论】:
呃。返回值没有什么特别之处。创建一个自动释放池,并在完成后释放它。如果有任何对象需要在池之外存在,请确保您拥有它们。 【参考方案1】:- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation, AddressAnnotation>) annotation
NSAutoreleasePool *myPool = [[NSAutoreleasePool alloc] init];
static NSString *reuseIdentifier = @"ident1";
BOOL needsRelease = NO;
// try to dequeue an existing pin view first
MKPinAnnotationView *pinView = [mapView dequeueReusableAnnotationViewWithIdentifier:reuseIdentifier];
if (!pinView)
// if an existing pin view was not available, create one
MKPinAnnotationView* pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
needsRelease = YES;
//customize the annotation
//...
//release the autorelease pool
[myPool drain];
//autorelease now if allocated new view - if autoreleased on the same line as alloc/inited, it would be released with the pool
if(needsRelease) [pinView autorelease];
return pinView;
【讨论】:
【参考方案2】:- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation, AddressAnnotation>) annotation
here if u create annotation view with alloc or init or copy then u have to release them manually.
remember every function inside which is not used with init or alloc or copy are autoreleased object.
so dont worry about them.
instead of pool u can create
//[annotationclass alloc] like this
then u can use like this to return.so no memory leak here
return [annonationView autorelease];
【讨论】:
【参考方案3】:这是如何正确执行带有返回值的自动释放池。
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation, AddressAnnotation>) annotation
id retVal;
NSAutoReleasePool *pool = [[NSAutoReleasePool alloc] init];
// do things here with the pool.
// save the return value to id retVal and it will be available later.
[pool release];
return retVal;
【讨论】:
【参考方案4】:- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation, AddressAnnotation>) annotation
NSAutoReleasePool *pool = [[NSAutoReleasePool alloc] init];
[pool drain];
return annonationView;
明白吗?
【讨论】:
释放池后他将如何使用该注解视图 通过重新调整 annonationView 我只是编写了代码用于演示。以上是关于目标 C:如何在方法具有返回类型的方法中或在具有任何返回类型的覆盖方法中设置 NSAutoreleasePool?的主要内容,如果未能解决你的问题,请参考以下文章