testing
testing

Reputation: 20279

Memory Management: Is insertSubview retaining it's views?

The question is if insertSubview is retaining the views and if I'm doing it right.

I would say yes. Since I'm not using the property anymore I don't receive an EXC_BAD_ACCESS. I think when releasing the view all subviews are also released. And so mapView is over-released. I'm right or do I still have a memory management issue?

My ViewController.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>

@interface MapViewController : UIViewController <MKMapViewDelegate> {

MKMapView *mapView;
// ...
}

//@property (nonatomic, retain) MKMapView *mapView;
// ...

@end

My ViewController.m

#import "MapViewController.h"

@implementation MapViewController

//@synthesize mapView;

- (void)viewDidLoad {
    [super viewDidLoad];

    //self.mapView=[[[MKMapView alloc] initWithFrame:self.view.bounds] autorelease];
    mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];

    [self.view insertSubview:mapView atIndex:0];
    [mapView release];

    // ...
}

- (void)dealloc {
    //[mapView release];
    [super dealloc];
}

@end

Upvotes: 1

Views: 878

Answers (3)

bbum
bbum

Reputation: 162712

- (void)dealloc {
    //[mapView dealloc];
    [super dealloc];
}

You should never call dealloc directly (save for [super dealloc]; at the end of the method). That will most assuredly cause a crash in most situations.


Since that isn't the source of your crash, you have an over-release somewhere. Use Instrument's Zombie Detection to figure out where.

Upvotes: 2

BWW
BWW

Reputation: 515

As a rule, you should not worry about whether or how another object will retain an instance you give it. That's up to that object to deal with; you only have to worry about making sure an instance that you intend to directly access later is retained. Don't rely on another object to keep an instance retained for you.

In your example, you have an instance (mapView) which is accessible to MapViewController but MapViewController does not have it's own retention for it. self.view could release mapView at any time for any number of reasons and you'd suddenly have bad memory there.

Upvotes: 2

highlycaffeinated
highlycaffeinated

Reputation: 19867

Yes, you are correct on all counts:

  1. the call to insertSubView: should be retaining the mapView that you are passing it.
  2. releasing your reference to the mapView after you add it to the parent view
  3. the parent view will release all the retained subviews when it is released

Upvotes: 2

Related Questions