Reputation: 40502
I have a custom UIView called TiledImage which has a single property named tiledImage and a custom drawRect method. I add this view to my ViewController, but when the ViewController is deallocated the dealloc for this view is never being called, even though I am releasing the view and setting it to nil. What could be causing this? I don't have any other references to the view, so my understanding is that it should release correctly.
This is how I add the view to my view controller:
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"tile" ofType:@"png"];
UIImage *image = [[UIImage alloc] initWithContentsOfFile:imagePath];
self.backImageView = [[TiledImage alloc] initWithFrame:IMAGE_FRAME];
self.backImageView.tiledImage = image;
[self.view addSubview:self.backImageView];
[self.view sendSubviewToBack:self.backImageView];
[image release];
And in my ViewController's dealloc method I have this:
_backImageView.tiledImage = nil, [_backImageView release], _backImageView = nil;
That line of code is hit, but dealloc is never called on TiledView. Note that _backImageView is the var that the property backImageView uses.
Can someone give me ideas on what I may be doing wrong that is preventing the dealloc on the TiledImage object from being called?
Upvotes: 3
Views: 4028
Reputation: 99092
If self.backImageView
is a retain
property, you have a memory leak - the TiledImage
has a retain count of 1 prior to invoking the setter, 2 afterwards.
Your code for adding the view should look like e.g. the following instead:
TiledImage *imageView = [[TiledImage alloc] initWithFrame:IMAGE_FRAME];
self.backImageView = imageView;
[imageView release];
Upvotes: 7