2011-07-05 32 views
0

如何在方法有返回類型的方法中設置NSAutoreleasePool? 有沒有辦法做到這一點? 像像下面的方法:Objective C:如何在方法有返回類型的方法中或者在任何返回類型的重載方法中設置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; 
    } 

所以應該是這樣嗎?

+0

呃。返回值沒有什麼特別之處。創建一個自動釋放池,並在完成後釋放它。如果任何物體需要超出泳池,請確保擁有它們。 –

回答

-2
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation, AddressAnnotation>) annotation { 
NSAutoReleasePool *pool = [[NSAutoReleasePool alloc] init]; 

[pool drain]; 
return annonationView; 

} 

明白嗎?

+0

發佈池之後他將如何使用該註釋視圖 –

+0

通過回調annonationView我只寫代碼來演示。 –

+0

哦對不起........ –

0
- (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; 

} 
0
- (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]; 

} 
0

這是如何正確地做一個autoreleasepool帶有返回值。

- (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; 
}