2011-07-05 3 views
0

NSAutoreleasePoolは、メソッドが戻り値の型を持つメソッド内でどのように設定できますか? これを行う方法はありますか?私は以下のようにmain.mファイルで見ることができます目的C:メソッドが戻り値の型を持つメソッド内でNSAutoreleasePoolを設定する方法、または戻り値の型でオーバーライドされたメソッド内にNSAutoreleasePoolを設定する方法は?

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated; 

:オーバーライドされたメソッド内

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation, AddressAnnotation>) annotation; 

など:以下のようなメソッドのような ので

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

Uh。値を返すことは特別なことはありません。自動解放プールを作成し、完了したら解放します。オブジェクトがプールを超えて存在する必要がある場合は、それらを所有していることを確認してください。 –

答えて

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

[pool drain]; 
return annonationView; 

} 

理解していますか?

+0

プールを解放した後、彼はそのannotationviewをどのように使用するのですか? –

+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; 
} 
関連する問題