iOS: Memory leak in simple MVC model - objective-c

I've build (must be simple...) MVC model, but I still have memory leak when pushing back button.
Model class: .h
#interface Nominal : NSObject {
int nominalID;
NSString *nominal;
NSString *nominalImg;
NSString *nominalName;
}
#property(nonatomic)int nominalID;
#property(nonatomic,retain)NSString *nominal;
#property(nonatomic,retain)NSString *nominalImg;
#property(nonatomic,retain)NSString *nominalName;
#end
.m
#implementation Nominal
#synthesize nominal,nominalID,nominalImg,nominalName;
-(void)dealloc
{
[self.nominal release];
[self.nominalImg release];
[self.nominalName release];
}
#end
I do release the strings as well.
In my view class I populate it so:
.h
#interface Nominals : UIViewController {
...
NSMutableArray *nominalsArr;
...
}
#property(retain,nonatomic)NSMutableArray *nominalsArr;
.m
- (void)viewWillAppear:(BOOL)animated
{
[[self navigationController]setToolbarHidden:YES animated:YES];
DBAccess *dbAccsess=[[DBAccess alloc]init];
self.nominalsArr=[dbAccsess returnNominals:subCountryID];
[dbAccsess closeDataBase];
[dbAccsess release];
[super viewWillAppear:animated];
}
- (void)dealloc
{
[nominalsArr release];
[self.navigationController release];
[super dealloc];
}
Looks like I do release the whole bundle of holy things, but when I push pack button from this view to previous, the memory leak pops up:
What I'm doing wrong?
You help is utterly appreciated.

You've forgotten a [super dealloc] in [Nominal -dealloc]. Also, don't call [self.navigationController release] as that property is already handled by the superclass (UIViewController).

Related

Passing value from one class to another, using #property

I have been pulling my hair out all afternoon trying to figure out why the following code will not work. All I am trying to do is pass a string, from one class to another.
In my FirstDetailViewController.h file I declare the NSString
#property(nonatomic, retain) NSString *infoForArray;
And then in my Grinding01_DetailViewController.m I try to set a value for the string
#import "Grinding01_DetailViewController.h"
#import "FirstDetailViewController.h"
#implementation Grinding01_DetailViewController
...
NSString *didLoadMessage = #"Grinding01 Loaded";
FirstDetailViewController *temp = [[FirstDetailViewController alloc] initWithNibName:#"FirstDetailView" bundle:nil];
temp.infoForArray = didLoadMessage;
[self.navigationController pushViewController:temp animated:YES];
}
When I output the infoForArray from the FirstDetailViewController.h it is null.
Any help would be appreciated, I think there's a simple step that I am missing, but I just can't see it.
EDIT: Here is the code from the FirstDetailViewController
FirstDetailViewController.h
#import <UIKit/UIKit.h>
#import "Protocols.h"
#interface FirstDetailViewController : UIViewController <SubstitutableDetailViewController> {
//for the output
IBOutlet UITextView *outputView;
UIToolbar *navigationBar;
NSMutableArray *logMessages;
}
#property (nonatomic, retain) IBOutlet UIToolbar *navigationBar;
//for incoming messages
#property(nonatomic, retain) NSString *infoForArray;
#end
FirstDetailViewController.m
#import "FirstDetailViewController.h"
#implementation FirstDetailViewController
#synthesize navigationBar, infoForArray;
-(void)viewDidLoad{
[super viewDidLoad];
//The log cannot be changed
outputView.editable = NO;
}
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidUnload {
[super viewDidUnload];
self.navigationBar = nil;
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:YES];
NSLog(#"message: %#", infoForArray);
outputView.text = infoForArray;
}
#pragma mark -
#pragma mark Managing the popover
- (void)showRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem {
// Add the popover button to the toolbar.
NSMutableArray *itemsArray = [navigationBar.items mutableCopy];
[itemsArray insertObject:barButtonItem atIndex:0];
[navigationBar setItems:itemsArray animated:NO];
[itemsArray release];
}
- (void)invalidateRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem {
// Remove the popover button from the toolbar.
NSMutableArray *itemsArray = [navigationBar.items mutableCopy];
[itemsArray removeObject:barButtonItem];
[navigationBar setItems:itemsArray animated:NO];
[itemsArray release];
}
#pragma mark -
#pragma mark Rotation support
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
#pragma mark -
#pragma mark Memory management
- (void)dealloc {
[navigationBar release];
[super dealloc];
}
#end
It sounds like the trouble area is in in FirstDetailViewController. I would suggest posting the code for that so we can see what's going on.
my guess is you are checking for infoForArray somewhere in the instantiation process of FirstDetailViewController, which occurs before you set temp.infoForArray = didLoadMessage.
Just for reference, if you check for infoForArray in viewDidLoad that will be too early. viewDidLoad is triggered when the view is put into memory. What you want is viewDidAppear, which you may have to add yourself

How to pass Arrays to a UIPickerView from one class to another?

Bah. I've pulled my hair out over this problem for the past couple days now, but I know I must be overlooking the obvious. I've made my PickerViewController(.h./m) and PickerViewAppDelegate(.h/.m) files and they run fine as a standalone program, but I would like to have the picker pop up after a procedureal event occurs in my "helloworld.m" file. I can get the picker to show up, but I cannot for the life of me figure out how to populate it so that it isn't blank. I THINK I've done everything right up until I try to pass my array to my pickerview object. What am I doing wrong?
PickerViewController.h
#import <UIKit/UIKit.h>
#interface PickerViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate> {
IBOutlet UIPickerView *pickerView;
NSMutableArray *scrollerData;
}
#property (nonatomic, retain) IBOutlet UIPickerView *pickerView;
#property (nonatomic, retain) NSMutableArray *scrollerData;
-(void)setScrollerData:(NSMutableArray *)array;
#end
PickerViewController.m
#import "PickerViewController.h"
#implementation PickerViewController
#synthesize pickerView, scrollerData;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
self.pickerView.delegate = self;
self.pickerView.dataSource = self;
}
- (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];
}
-(void)setScrollerData:(NSMutableArray *)array
{
//[self.scrollerData arrayByAddingObjectsFromArray:array];
scrollerData = array;
}
#pragma mark -
#pragma mark Picker View Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
return [scrollerData count];
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [scrollerData objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
NSLog(#"Selected Number: %#. Index of selected numbers: %i", [scrollerData objectAtIndex:row], row);
}
PickerViewAppDelegate.h
#import <UIKit/UIKit.h>
#class PickerViewController;
#interface PickerViewAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
PickerViewController *pvController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#end
PickerViewAppDelegate.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
Helloworld.m
...
UIView* view = [[CCDirector sharedDirector] openGLView];
UIPickerView *pickerView=[[UIPickerView alloc] init];
pickerView.frame=CGRectMake(100,100, 200, 200);
NSMutableArray *arrayNumbers = [[NSMutableArray alloc] init];
[arrayNumbers addObject:#"30"];
[arrayNumbers addObject:#"31"];
[arrayNumbers addObject:#"32"];
[arrayNumbers addObject:#"33"];
[arrayNumbers addObject:#"34"];
[arrayNumbers addObject:#"35"];
[arrayNumbers addObject:#"36"];
[pickerView setscrollerData: arrayNumbers];//Should I be calling pickerView here or something else?
[view addSubview: pickerView];
pickerView.hidden=NO;
...
You have overridden the setter method generated by #synthesize in PickerViewController, so you are no longer retaining it.
Then, you are calling setScrollerData on your pickerView (this should be giving you a warning or crashing since pickerView doesn't respond to that method).
You are not setting PickerViewController as the delegate or datasource of your picker view in helloworld.m.
I can't see where your hello world code fits in. It seems to be adding a new picker view rather than using the one from the xib of PickerViewController. You should be instantiating pickerviewcontroller from your hello world and adding its .view as a subview or presenting it as a modal view controller rather than setting up a new picker view. You can then pass your array to the instance of pickerviewcontroller. Note though that it is not standard to have a separate view controller for what is essentially a subview, though I don't have much knowledge of cocos2d so I don't know if this is normal when using that framework.
Well, i think you should just pass the array from HelloWorld class to PickerViewController class using property/synthesize.

What am I doing wrong with my code, when trying to create an email in iOS? (Help me understand protocols/delegates)

I'm trying to take my "myEmail" class and have it be my "all email methods go here" class,
and any other class that will email, will use "myEmail". "myEmail" must include MessageUI framework, and
then instiantiate an email controller to animate on the screen.
The complication is that I don't understand how to use "myEmail", which uses "MFMailComposeViewController",
in my "Documents" view correctly. When I call "sendEmail" in my "Documents" class, it never shows the
email window slide into view.
I understand that I can cut out myEmail as the middle man and use the MessageUI framework methods right
in Documents view, but I don't want to go about it in that way.
If anyone could point out how I'm using protocols/delegates wrong, I'd really appreciate it.
Code in Question
This is my myEmail class
In myEmail.h:
#import <Foundation/Foundation.h>
#import <MessageUI/MessageUI.h>
#protocol myEmailDelegate <MFMailComposeViewControllerDelegate>
#required
-(void)sendEmail;
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error;
#end
#interface myEmail : MFMailComposeViewController {
id <myEmailDelegate> delegate;
}
#property(nonatomic,assign) id<MFMailComposeViewControllerDelegate> myEmailDelegate;
#end
In myEmail.m:
#import "myEmail.h"
#import "ConstructionDocuments.h"
#implementation myEmail
#synthesize myEmailDelegate;
-(void)sendEmail
{
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"mypdfdoc" ofType:#"pdf"];
NSData *myData = [NSData dataWithContentsOfFile:filePath];
MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = myEmailDelegate;
[controller setSubject:#"Email Example"];
[controller setMessageBody:#"Attached is pdf." isHTML:NO];
[controller addAttachmentData:myData mimeType:#"application/pdf" fileName:filePath];
[self presentModalViewController:controller animated:YES];
[controller release];
}
- (void)addAttachmentData:(NSData*)attachment mimeType:(NSString*)mimeType fileName:(NSString*)filename
{
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
[self becomeFirstResponder];
[self dismissModalViewControllerAnimated:YES];
}
- (void) dealloc
{
[myEmailDelegate release];
[super dealloc];
}
#end
This is the View where I'll be using my "myEmail" class
In Documents.h:
#import <UIKit/UIKit.h>
#import "myEmail.h"
#interface Documents : UIViewController <myEmailDelegate> {
}
#property(nonatomic, assign) id<MFMailComposeViewControllerDelegate> myEmailDelegate;
- (IBAction)sendEmail;
#end
#protocol myEmailDelegate <myEmailDelegate>
- (void) sendEmail;
#end
In Documents.m:
- (IBAction)sendEmail
{
myEmail *mymyEmail = [[myEmail alloc] init];
[mymyEmail setmyEmailDelegate: myEmailDelegate];
[myEmailDelegate sendEmail];
}
IIRC you need to call presentModalViewController: on an "active" view controller which is not the case for you. You could do something like this:
-(void)sendEmail:(UIViewController *)externalController
{
// ...
[externalController presentModalViewController:controller animated:YES];
// ...
}
And then call it from your action (which seems to be inside a view controller):
- (IBAction)sendEmail
{
myEmail *mymyEmail = [[myEmail alloc] init];
[mymyEmail setmyEmailDelegate:myEmailDelegate];
[mymyEmail sendEmail:self];
}
BTW, class names start with uppercase letter by convention.

EXC_BAD_ACCESS when I change moviePlayer contentURL

In few words, my application is doing that :
1) My main view (RootViewController) has a buton when I tap on it, it displays the player (PlayerViewController) :
2) In my Player, I initialize the video I want to play
-> It's working good, my movie is display
My problem :
When I go back to my main view :
And I tap again on the button, I get a *Program received signal: “EXC_BAD_ACCESS”.*
If I comment self.player.contentURL = [self movieURL]; it's working, but when I let it, iI have this problem.
I read that it's due to null pointer or memory problem but I don't understand why it's working the first time and not the second time. I release my object in dealloc method.
Thanks for your help !
Bruno.
Here is my code :
Root View Controller
RootViewController.h
#import <UIKit/UIKit.h>
#import "PlayerViewController.h"
#interface RootViewController : UIViewController {
IBOutlet UIButton * myButton;
}
#property (nonatomic,retain) IBOutlet UIButton * myButton;
-(IBAction)displayPlayer:(id)sender;
- (void) returnToRoot: (PlayerViewController *) controller;
#end
RootViewController.m
#import "RootViewController.h"
#implementation RootViewController
#synthesize myButton;
-(IBAction)displayPlayer:(id)sender
{
PlayerViewController *playerViewController = [[PlayerViewController alloc] initWithNibName:#"PlayerViewController" bundle:nil];
playerViewController.delegate = self;
playerViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController: playerViewController animated: YES];
[playerViewController release];
}
- (void) returnToRoot: (PlayerViewController *) controller
{
[self dismissModalViewControllerAnimated: YES];
}
- (void)viewDidUnload {
[super viewDidUnload];
}
- (void)dealloc {
[super dealloc];
}
#end
Player View Controller
PlayerViewController.h
#import <UIKit/UIKit.h>
#import <MediaPlayer/MPMoviePlayerController.h>
#protocol PlayerViewControllerDelegate;
#interface PlayerViewController : UIViewController {
UIView *viewForMovie;
MPMoviePlayerController *player;
}
#property (nonatomic, assign) id <PlayerViewControllerDelegate> delegate;
#property (nonatomic, retain) IBOutlet UIView *viewForMovie;
#property (nonatomic, retain) MPMoviePlayerController *player;
- (NSURL *)movieURL;
-(IBAction)goBackToRoot:(id)sender;
#end
#protocol PlayerViewControllerDelegate
- (void) returnToRoot: (PlayerViewController *) controller;
#end
PlayerViewController.m
#import "PlayerViewController.h"
#implementation PlayerViewController
#synthesize player;
#synthesize viewForMovie;
#synthesize delegate;
- (void)dealloc {
[super dealloc];
[player release];
[viewForMovie release];
}
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(#"viewDidLoad");
self.player = [[MPMoviePlayerController alloc] init];
[self.player autorelease];
self.player.view.frame = self.viewForMovie.bounds;
self.player.view.autoresizingMask =
UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight;
[self.viewForMovie addSubview:player.view];
self.player.contentURL = [self movieURL];
[self.player play];
}
-(NSURL *)movieURL
{
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath =
[bundle
pathForResource:#"myVideo"
ofType:#"mp4"];
if (moviePath) {
return [NSURL fileURLWithPath:moviePath];
} else {
return nil;
}
}
-(IBAction)goBackToRoot:(id)sender{
[self.delegate returnToRoot: self];
}
- (void)viewDidUnload {
[super viewDidUnload];
}
#end
Problem
The second time I call "displayPlayer" I had the EXC_BAD_ACCESS
I solved it !!!
I look on the MPMoviePlayerController to see what kind of variable is contentURL
(NSURL *)contentURL
It means I have also to liberate it.
I do that in my dealloc method putting a nil value:
-(void) dealloc {
[super dealloc];
self.player.contentURL = nil;
[player release];
[viewForMovie release];
}
If I comment self.player.contentURL =
[self movieURL]; it's working, but
when I let it, iI have this problem.
In that case, how is contentURL declared? Does the #property definition include copy or retain?

iPhone delegate & controller dealloc?

I have been playing with a simple iphone app and decided to put an NSLog statement in the deallocs of both the controller and the delegate but neither of these print out to the Xcode console?
// APPLICATION DELEGATE
#import "iPhone_buttonFunAppDelegate.h"
#import "iPhone_buttonFunViewController.h"
#implementation iPhone_buttonFunAppDelegate
#synthesize window;
#synthesize viewController;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after app launch
NSLog(#"applicationDidFinishLaunching ...");
[window addSubview:viewController.view];
[window makeKeyAndVisible];
}
- (void)applicationWillTerminate:(UIApplication *)application {
NSLog(#"AWT:");
}
- (void)dealloc {
NSLog(#"-DEL-"); // << Does not print?
[viewController release];
[window release];
[super dealloc];
}
#end
// VIEW CONTROLLER
#import "iPhone_buttonFunViewController.h"
#implementation iPhone_buttonFunViewController
#synthesize statusText;
-(IBAction)buttonPressed:(id) sender {
NSString *title;
NSString *newText;
title = [sender titleForState:UIControlStateNormal];
newText = [[NSString alloc] initWithFormat:#"%# button pressed.", title];
[statusText setText:newText];
[newText release];
NSLog(#"Button Press ... %#", title);
}
-(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.
NSLog(#"-1-");
}
-(void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
NSLog(#"-2-");
self.statusText = nil;
}
-(void)dealloc {
NSLog(#"-CON-"); // << Does not print?
[statusText release];
[super dealloc];
}
#end
gary
This is an optimization in the Cocoa touch runtime. Certain deallocations aren't made at the end of the program, since the entire program is going to exit and they will be wiped out anyway.
This problem with NSLog(...) may be answered by this other stackoverflow question about applicationWillTerminate:
Good luck.