0

How can I set up a NSAutoreleasePool within a method where the method have a return type? Is there any way to do that? like the methods like below:

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

Or a within a overridden method like:

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

I can see in the main.m file like below:

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

So it should be like this?

4

4 回答 4

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];

}
于 2011-07-05T12:34:46.950 回答
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;

}
于 2011-07-05T12:27:31.600 回答
0

这是如何正确执行带有返回值的自动释放池。

- (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;
}
于 2012-01-12T08:51:43.707 回答
-2
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation, AddressAnnotation>) annotation {
NSAutoReleasePool *pool = [[NSAutoReleasePool alloc] init];

[pool drain];
return annonationView;

}

理解?

于 2011-07-05T12:00:34.713 回答