Reputation: 20279
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
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.
Upvotes: 2
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
Reputation: 19867
Yes, you are correct on all counts:
insertSubView:
should be retaining the mapView that you are passing it. Upvotes: 2