Why is my loaded nib crashing when memory is released? - iphone

If I run Build and Analyze this loads without errors according to the Analyzer but the app crashes. If I remove the [myStates release]; analyzer complains about possible leak but the nib loads and runs just fine. MyStateList is a nib which has a pickerview inside that loads a plist if this helps. Please help.
Main TrialViewControllerViewController Implementation File
#import "TrialViewControllerViewController.h"
#import "MyStateList.h"
#implementation TrialViewControllerViewController
- (void)viewDidLoad {
[super viewDidLoad];
MyStateList *myStates = [[MyStateList alloc] initWithNibName:#"MyStateList" bundle:nil];
[self.view addSubview:[myStates view]];
//[myStates release];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
}
- (void)dealloc {
[super dealloc];
}
#end
Here is what i am trying to load
MyStateList.h
#import <UIKit/UIKit.h>
#interface MyStateList : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource> {
UIPickerView *pickerView;
NSMutableArray *statesArray; // Written array
NSDictionary *stateListDictionary,*stateListDictionaries;
NSArray *firstDisplayArray,*updateDisplayArray,*switchDisplayArray;
NSInteger dTag;
NSString *nott,*verify,*notes;
IBOutlet UILabel *labelOne,*labelTwo,*labelThree,*labelName;
}
#property (nonatomic, retain) UIPickerView *pickerView;
#property (nonatomic, retain) NSDictionary *stateListDictionary,*stateListDictionaries;
#property (nonatomic, retain) NSArray *firstDisplayArray,*updateDisplayArray,*switchDisplayArray;
#property (nonatomic, retain) IBOutlet UILabel *labelOne,*labelTwo,*labelThree,*labelName;
#property (nonatomic, retain) NSString *nott,*verify,*notes;
- (void)loadData;
- (void)placeData;
- (void)createPicker;
#end
MyStateList.m
#import "MyStateList.h"
#implementation MyStateList
#synthesize pickerView;
#synthesize stateListDictionary,stateListDictionaries,firstDisplayArray,updateDisplayArray,switchDisplayArray ;
#synthesize labelOne,labelTwo,labelThree,labelName;
#synthesize nott,verify,notes;
- (void)viewDidLoad
{
[super viewDidLoad];
[self createPicker];
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [statesArray count];
}
-(UIView *)pickerView:(UIPickerView *)pickerViewCust viewForRow:(NSInteger)row forComponent: (NSInteger)component reusingView:(UIView *)view
{
NSString *rowItem = [statesArray objectAtIndex: row];
UILabel *lblRow = [[[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerViewCust bounds].size.width, 44.0f)]autorelease];
[lblRow setTextAlignment:UITextAlignmentCenter];
[lblRow setTextColor: [UIColor blueColor]];
[lblRow setText:rowItem];
[lblRow setBackgroundColor:[UIColor clearColor]];
return lblRow;
}
- (void)createPicker
{
NSString *path = [[NSBundle mainBundle] pathForResource:
#"StateArray" ofType:#"plist"];
stateListDictionary = [NSDictionary dictionaryWithContentsOfFile:path];
labelName.text = [NSString stringWithFormat:#"Arizona"];
[self loadData];
float screenWidth = [UIScreen mainScreen].bounds.size.width;
float pickerWidth = screenWidth * 1 / 2;
float xPoint = screenWidth / 2 - pickerWidth / 1;
pickerView = [[UIPickerView alloc] init];
[pickerView setDataSource: self];
[pickerView setDelegate: self];
[pickerView setFrame: CGRectMake(xPoint, 280.0f, pickerWidth, 180.0f)];
pickerView.showsSelectionIndicator = YES;
[pickerView selectRow:2 inComponent:0 animated:YES];
[self.view addSubview: pickerView];
}
- (void)loadData
{
firstDisplayArray = [stateListDictionary objectForKey:#"Arizona"];
dTag = 1;
[self placeData];
stateListDictionary = nil; // kill the list
statesArray = [[NSMutableArray alloc] init];
[statesArray addObject:#"Alabama"];
[statesArray addObject:#"Alaska"];
[statesArray addObject:#"Arizona"];
[statesArray addObject:#"Arkansas"];
[statesArray addObject:#"California"];
}
- (void)placeData
{
if (dTag == 1)
{
switchDisplayArray = firstDisplayArray;
dTag = 0;
} else {
switchDisplayArray = updateDisplayArray;
}
nott = [switchDisplayArray objectAtIndex:0];
verify = [switchDisplayArray objectAtIndex:1];
notes = [switchDisplayArray objectAtIndex:2];
labelOne.text = nott;
labelTwo.text = verify;
labelThree.text = notes;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent: (NSInteger)component
{
NSString *path = [[NSBundle mainBundle] pathForResource:
#"StateArray" ofType:#"plist"];
stateListDictionaries = [NSDictionary dictionaryWithContentsOfFile:path];
labelName.text = [statesArray objectAtIndex: row];
updateDisplayArray = [stateListDictionaries objectForKey:labelName.text];
[self placeData];
}
- (void)dealloc
{
[pickerView release];
[stateListDictionary release];
[stateListDictionaries release];
[statesArray release];
[super dealloc];
}
#end

If I had to guess, MyStatesList is a subclass of UIViewController.
If you release myStates at the end of the block, you're essentially removing the brain from your view controller, but leaving its body there. You need to keep the controller itself around as well as the view, as the view is owned by the controller. Not the other way around.
The view from your view controller is retained by the view, but you killed the view controller itself. The bigger problem is you CAN'T just add views from UIViewControllers in this manner. On iOS5 you have the ability to use addChildViewController: and prior to that you can use one of the provided container controllers.

You do indeed need to release your MyStateList object.
Here's what's happening:
You initialize a new MyStateList, which has a retain count of 1
You add a subview to your view, and that subview is NOT the MyStateList object, but the ivar view of your MyStateList object.
When you don't release your myStates object, you are indeed leaking this object, since nothing has a reference to this object anymore, and the memory was never deallocated. (Its retain count never reached 0.)
That having been said, what kind of error occurs when your application crashes? I'm guessing that MyStateList is a UIViewController, and the view is then attempting to access/communicate with its parent, which has been released/deallocated.

In your -(void)dealloc; you are calling [super dealloc] first, this is wrong. Make it the last call in the method. This should fix your crash.
- (void)dealloc
{
// Wrong Way
[super dealloc];
[pickerView release];
[stateListDictionary release];
[stateListDictionaries release];
[statesArray release];
}
`
- (void)dealloc
{
// Right way
[pickerView release];
[stateListDictionary release];
[stateListDictionaries release];
[statesArray release];
[super dealloc];
}

you add the view [myStates view], to the controller's view, so the view is hold by the controller's view.
then you release "myStates" for avoid memeory leak, it is removed.
and I guess in your view [myStates view], there must be some delegates related to the object "myState", but it was removed just before. So, the app crash.
To avoid both problems, memory leak and crash. I think you should make the "myStates" hold by your controller as a member variable. And you could release "myState" when your viewUnloaded.

Related

release object of a return method object c

in run the app with the analyze build, and Xcode found me a lot of memory leak and there is one in particular that i don't know how solve here it is:
- (UIView *) tableView:(UITableView *)tableView
viewForHeaderInSection:(NSInteger)section {
UIImageView *sectionImage = [[UIImageView alloc] init];
if (section == 0)sectionImage.image = [UIImage imageNamed:#"myImage.png"];
return sectionImage;
}
so my question is, how i can release this sectionImage? if is the return of the method?
EDIT:
i have another question, analyze give me another memory leak, i have this:
.h
#property (nonatomic, retain) NSIndexPath *directCellPath;
.m
#synthesize directCellPath = _directCellPath;
- (id)init{
if ((self = [super initWithNibName:#"MyViewController" bundle:nil])) {
self.directCellPath = [[NSIndexPath alloc] init];
}
return self;
}
then in the code i use it and finally in the dealloc i do this:
- (void)dealloc {
[_directCellPath release];
[super dealloc];
}
and give me a memory leak on this line:
self.directCellPath = [[NSIndexPath alloc] init];
why if i have deallocated it in the dealloc?
you must just use an autorelease like this
UIImageView *sectionImage = [[[UIImageView alloc] init] autorelease];
To answer your second question, you released _directCellPath, not directCellPath. There is a difference.
you must do this
#synthesize directCellPath
and this
- (void)dealloc
{
self.directCellPath = nil;
[directCellPath release];
[super dealloc];
}

Problem Exc_Arithmetic in Obj-C program for iphone

I am writing a program with a UIImageView. However, I am running into the error: Exc_Arithmetic.
Here is my implementation:
#import "SalaatAppViewController.h"
#implementation SalaatAppViewController
#synthesize imageView;
#synthesize pauseButton;
#synthesize playButton;
#synthesize nextButton;
-(IBAction)pushPauseButton {
[imageView stopAnimating];
}
-(IBAction)pushPlayButton {
[imageView startAnimating];
}
- (IBAction)pushNextButton {
imagesIndex = (imagesIndex + 1) % [images count];
imageView.image = [images objectAtIndex:imagesIndex];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
imageView.animationImages = [[NSArray arrayWithObjects:
[UIImage imageNamed:#"1.png"],
[UIImage imageNamed:#"2.png"],
[UIImage imageNamed:#"3.png"],
nil] retain];
UIImage *image = [UIImage imageNamed: #"2.png"];
[imageView setImage:image];
imageView.animationDuration = 1.00;
imageView.animationRepeatCount = 0;
[imageView startAnimating];
imagesIndex = 0;
imageView.image = [images objectAtIndex:imagesIndex];
[super viewDidLoad];
[self.view addSubview:imageView];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(void) dealloc {
[imageView release];
[pauseButton release];
[playButton release];
[nextButton release];
[super dealloc];
}
#end
My Header:
#import <UIKit/UIKit.h>
#interface SalaatAppViewController : UIViewController {
UIImageView *imageView;
UIButton *pauseButton;
UIButton *playButton;
UIButton *nextButton;
NSUInteger images;
NSUInteger imagesIndex;
}
#property (nonatomic, retain) IBOutlet UIImageView *imageView;
#property (nonatomic, retain) IBOutlet UIButton *pauseButton;
#property (nonatomic, retain) IBOutlet UIButton *playButton;
#property (nonatomic, retain) IBOutlet UIButton *nextButton;
-(IBAction)pushPauseButton;
-(IBAction)pushPlayButton;
-(IBAction)pushNextButton;
#end
The program runs, but with 9 warnings and 1 error. Also, when I push the Next Button, it crashes. Please Help!
The exception indicates that you tried to divide by 0. This expression does a modulus (%) function which returns remainder after division, probably [images count] is 0:
imagesIndex = (imagesIndex + 1) % [images count];
Ahmad your "images" property should be not NSUInteger but NSArray*. Define it in a following way:
NSArray *images;
It seems you replaced too much by following my previous guidance. =)
You never initialize images. Also, as you're calling the count method, it seems that it should be an NSArray and not an NSUInteger.

UIPickerview AppDelegate problem with subviews

Issue with subviews but it may be related to the AppDelegate with respect to placing a UIPickerView in myView as apposed to self.view?
Thanks
[self.view addSubview:pickerView]; <-- works ok
//[myView addSubview:pickerView];<-- fails, can't get dial movement or response
AppDelegate.h
#import <UIKit/UIKit.h>
#class PickerViewController;
#interface PickerViewAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
PickerViewController *pvController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#end
AppDelegate.m
#import "PickerViewAppDelegate.h"
#import "PickerViewController.h"
#implementation PickerViewAppDelegate
#synthesize window;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
pvController = [[PickerViewController alloc] initWithNibName:#"PickerView" bundle:[NSBundle mainBundle]];
[window addSubview:pvController.view];
// Override point for customization after application launch
[window makeKeyAndVisible];
}
- (void)dealloc {
[pvController release];
[window release];
[super dealloc];
}
#end
Controller.h
#import <UIKit/UIKit.h>
#interface PickerViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate> {
IBOutlet UIPickerView *pickerView;
NSMutableArray *arrayColors;
}
#end
Controller.m
#import "PickerViewController.h"
#implementation PickerViewController
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 100, 100, 100)];
[myView setBackgroundColor:[UIColor blueColor]];
[self.view addSubview:myView];
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 0.0, 100.0, 100.0)];
pickerView.delegate = self;
pickerView.showsSelectionIndicator = YES;
arrayColors = [[NSMutableArray alloc] init];
for (int i=0; i<60; i++) {
[arrayColors addObject:[NSString stringWithFormat:#"%d", i]];
}
//[self.view addSubview:pickerView]; // <-- works
[myView addSubview:pickerView]; // <-- this fails.. this is what I need for multiple views
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}
- (void)dealloc {
[arrayColors release];
[super dealloc];
}
#pragma mark -
#pragma mark Picker View Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
return [arrayColors count];
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [arrayColors objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
NSLog(#"Selected Color: %#. Index of selected color: %i", [arrayColors objectAtIndex:row], row);
}
#end
If you comment
[myView addSubview:pickerView];
you will understand, that myView frame is too small. You will be able to interact with the picker only touching that parent view frame. If you want to make the pickers behave normally, you must set the parent view frame to contain the picker frame - make it equal or larger.
Please remember iOs Human Interface Guidelines,
The overall size of a picker, including its background, is fixed at the same size as the keyboard on iPhone.
Make sure that myView has userInteractionEnabled set to YES?

UIImagePickerController and UINavigationController

Well, i have 2 view controllers the fist has three buttons each one represents an image on an array of images. When a user presses a button moves on the second view controller which has just an IBOutlet UIImageView to view the image. How can i do this programming trick using the UIImagePickerController? here are my files:
// PhotoListViewController.h
#import <UIKit/UIKit.h>
#import "PhotoDetailViewController.h"
#interface PhotoListViewController : UIViewController {
IBOutlet UIButton *button;
IBOutlet UIImageView *image1;
IBOutlet UIImageView *image2;
IBOutlet UIImageView *image3;
IBOutlet UILabel *label1;
IBOutlet UILabel *label2;
IBOutlet UILabel *label3;
IBOutlet UILabel *nameMikro1;
IBOutlet UILabel *nameMikro2;
IBOutlet UILabel *nameMikro3;
NSString *nameMikroProp;
IBOutlet PhotoDetailViewController *photoDetailViewController;
}
#property (nonatomic, retain) PhotoDetailViewController *photoDetailViewController;
-(IBAction)showImage:(id)sender;
#property (copy) NSString *nameMikroProp;
#end
// PhotoListViewController.m
#import "PhotoListViewController.h"
#import "PhotoDetailViewController.h"
#implementation PhotoListViewController
#synthesize nameMikroProp;
#synthesize photoDetailViewController;
-(IBAction)showImage:(id)sender{
photoDetailViewController = [[PhotoDetailViewController alloc]init];
//photoDetailViewController.delegate = self;
if([sender tag] == 4){
//[photoDetailViewController.imageViewprop setImage:[UIImage imageNamed:#"zaab.png"]];
//[self presentModalViewController:self.imgPicker animated:YES];
}
else if([sender tag] == 5){
}
else{
}
[self.navigationController pushViewController:photoDetailViewController animated:YES];
[photoDetailViewController release];
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
NSMutableArray *imageArray = [[NSMutableArray alloc] init];
[imageArray addObject:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"main" ofType:#"jpg"]]];
[imageArray addObject:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"main2" ofType:#"jpg"]]];
[imageArray addObject:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"main3" ofType:#"jpg"]]];
NSMutableArray *nameArray = [[NSMutableArray alloc] init];
[nameArray addObject:#"zaab's photo one"];
[nameArray addObject:#"zaab's photo two"];
[nameArray addObject:#"zaab's photo three"];
nameMikro1.text = nameMikroProp;
nameMikro2.text = nameMikroProp;
nameMikro3.text = nameMikroProp;
if([#"zaab" isEqualToString:nameMikro1.text]) {
[image1 setImage:[imageArray objectAtIndex:0]];
[image2 setImage:[imageArray objectAtIndex:1]];
[image3 setImage:[imageArray objectAtIndex:2]];
[label1 setText:[nameArray objectAtIndex:0]];
[label2 setText:[nameArray objectAtIndex:1]];
[label3 setText:[nameArray objectAtIndex:2]];
}
else if([#"evza" isEqualToString:nameMikro1.text]) {
[image1 setImage:[UIImage imageNamed:#"evza.jpg"]];
}
[imageArray release];
[nameArray release];
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[nameMikro1 release];
[nameMikro2 release];
[nameMikro3 release];
[label1 release];
[label2 release];
[label3 release];
[image1 release];
[image2 release];
[image3 release];
[super dealloc];
}
#end
// PhotoDetailViewController.h
#import <UIKit/UIKit.h>
#interface PhotoDetailViewController : UIViewController<UINavigationControllerDelegate, UIImagePickerControllerDelegate> {
IBOutlet UIImageView *imageViewprop;
UIImagePickerController *imgPicker;
}
#property (nonatomic, retain) UIImageView *imageViewprop;
#property (nonatomic, retain) UIImagePickerController *imgPicker;
- (IBAction)grabImage;
#end
// PhotoDetailViewController.m
#import "PhotoDetailViewController.h"
#implementation PhotoDetailViewController
#synthesize imageViewprop;
#synthesize imgPicker;
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {
imageViewprop.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"main" ofType:#"jpg"]];
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
}
- (IBAction)grabImage {
[self presentModalViewController:self.imgPicker animated:YES];
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
self.imgPicker = [[UIImagePickerController alloc] init];
self.imgPicker.allowsImageEditing = NO;
self.imgPicker.delegate = self;
self.imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
#end
Why do you need an image picker controller to show an image? ImagePickerController is used to pick an image from the album or by using camera.
What you can do is to pass the image from the PhotoListViewController to PhotoDetailViewController.
In the PhotoDetailViewController, you can have a property of UIImage (synthesized), so that you can set it before loading it. Then in the PhotoDetailVC use a UIImageView to show the image.
If you are looking for more advance functionality with image, look at the three20 framework at
three20 at github.

Popping View crashes Application at [super dealloc]

I have a tableView:didSelectRowAtIndexPath: where I create a ViewController-Instance each time an item is selected. Sometimes it works fine for a long time, sometimes it crashes with a EXC_BAD_ACCESS very quickly. The debugger blames the line [super dealloc]; in the QuestionDetailViewController.
Why? I log the QuestionDetailViewController retainCount. That looks fine.
QuestionDetailViewController
#import <UIKit/UIKit.h>
#import "Question.h"
#import "Answer.h"
#interface QuestionDetailViewController : UIViewController < UIScrollViewDelegate , QuestionDetailViewProtocol> {
Question *question;
UILabel *answerText;
UILabel *titleLabel;
}
#property(nonatomic,retain) Question *question;
#property(nonatomic,retain) UILabel *answerText;
#property(nonatomic,retain) UILabel *titleLabel;
#end
#import "QuestionDetailViewController.h"
#implementation QuestionDetailViewController
#synthesize question;
#synthesize answerText;
#synthesize titleLabel;
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)viewWillAppear:(BOOL)animated {
UIColor *bgColor = [UIColor colorWithRed:199.0/255 green:234.0/255 blue:251.0/255 alpha:1];
self.answerText = (UILabel *)[self.view viewWithTag:2];
self.answerText.backgroundColor = bgColor;
self.answerText.text = self.question.answer.text;
self.titleLabel = (UILabel*)[self.view viewWithTag:1];
CGSize sizeTitle = [self.question.title sizeWithFont:[titleLabel font]
constrainedToSize:CGSizeMake(280.0, INFINITY)
lineBreakMode:UILineBreakModeWordWrap];
self.titleLabel.text = self.question.title;
self.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
self.titleLabel.frame = CGRectMake(10,10, 260, sizeTitle.height);
CGSize sizeText = [self.question.answer.text sizeWithFont:[self.answerText font]
constrainedToSize:CGSizeMake(280.0, INFINITY)
lineBreakMode:UILineBreakModeWordWrap];
self.answerText.frame = CGRectMake(0,20+sizeTitle.height, 310, sizeText.height+sizeTitle.height);
[(UIScrollView *)self.view setContentSize:CGSizeMake(280, answerText.frame.size.height +sizeTitle.height)];
self.view.backgroundColor = bgColor;
[super viewWillAppear:animated];
}
- (void)viewDidDisappear:(BOOL)animated {
NSLog(#"%d", [self retainCount]);
[super viewDidDisappear:animated];
[self release];
}
-(IBAction)goBack:(id)sender{
[self.navigationController popViewControllerAnimated:YES];
}
- (void)dealloc {
NSLog(#"QDC dealoc");
[self.answerText release];
[self.titleLabel release];
[self.question release];
[super dealloc];
}
#end
tableView:didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
QuestionDetailViewController *qdc = [[QuestionDetailViewController alloc] initWithNibName:#"QuestionDetailViewController" bundle:nil];
Question* question;
if ([filteredQuestions count]>0) {
question = [self.filteredQuestions objectAtIndex:indexPath.row];
} else {
question = [self.questions objectAtIndex:indexPath.row];
}
[qdc setQuestion:question];
[question release];
NSLog(#"%d", [qdc retainCount]);
[self.navigationController pushViewController:qdc animated:YES];
[qdc release];
}
This:
- (void)viewDidDisappear:(BOOL)animated {
NSLog(#"%d", [self retainCount]);
[super viewDidDisappear:animated];
[self release];
}
looks incredibly suspicious. Did you do [self retain]? If not, then why are you releasing? If you are, you're probably doing something wrong, because [self retain] is an incredibly rare thing to need to do.
Instead of:
[self.answerText release];
[self.titleLabel release];
[self.question release];
use:
self.answerText = nil;
self.titleLabel = nil;
self.question = nil;
or:
[answerText release]; answerText = nil;
[titleLabel release]; titleLabel = nil;
[question release]; question = nil;