开发者

Abandoned memory issue

Could you help me out please, my app is almost finished, I'm now in the process of optimizing it so I'm using the Instrument Allocations tools and I have a problem vith a view controller but I can't figure out why memory increases each time I go from the main view to the detailedViewController.

Here is the code part that seems to be the problem :

  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

// Create and push a detail view controller.

    self.entriesDetailedViewController = [[EntriesDetailedViewController alloc]init];

Entry *selectedEntry = (Entry *)[[self fetchedResultsController] objectAtIndexPath:indexPath];

self.entriesDetailedViewController.entry = selectedEntry;

[self.navigationController pushViewController:self.entriesDetailedViewController animated:YES];
[self.entriesDetailedViewController release];
}

Here is the code for the detailedViewController.h:

  #import <UIKit/UIKit.h>


  @class Entry;

 @interface EntriesDetailedViewController : UIViewController <UITextViewDelegate> {

//IBOutlet UIButton *createEntryButton;
IBOutlet UITextField *entryTextField1;
IBOutlet UITextView *entryTextField2;
IBOutlet UIBarButtonItem *textbodyBarButton;
IBOutlet UIBarButtonItem * catLabel;
IBOutlet UINavigationBar *entryNameToolBar;
IBOutlet UINavigationBar *textBodyToolBar;
IBOutlet UIImageView *reviewCheck;
IBOutlet UIImageView *textBackground;
IBOutlet UIBarButtonItem *reviewButton;
BOOL isChecked;

NSManagedObjectContext *managedObjectContext;


Entry *entry;

    }


 @property (nonatomic,retain) IBOutlet UITextField *entryTextField1;
 @property (nonatomic,retain) IBOutlet UITextView *entryTextField2;
 @property (nonatomic,retain) IBOutlet UIBarButtonItem *textbodyBarButton;
 @property (nonatomic,retain) IBOutlet UIBarButtonItem *catLabel;
 @property (nonatomic,retain) IBOutlet UINavigationBar *entryNameToolBar;
 @property (nonatomic,retain) IBOutlet UINavigationBar *textBodyToolBar;
 @property (nonatomic,retain) IBOutlet UIImageView *reviewCheck;
 @property (nonatomic,retain) IBOutlet UIBarButtonItem *reviewButton;
 @property BOOL isChecked;
 @property (nonatomic,retain) IBOutlet UIImageView *textBackground;

 @property (nonatomic,retain) NSManagedObjectContext *managedObjectContext;
 @property (nonatomic, retain) Entry *entry;


 - (void)setUpUndoManager;
 - (void)cleanUpUndoManager;
 - (void)textViewDidBeginEditing:(UITextView *)entryTextField2;
 - (void)textViewDidEndEditing:(UITextView  *)entryTextField2;
 - (void)saveContext;



 - (IBAction)dismisskeyboard;
 - (IBAction)dismissKeyboardfromTextView;
 - (IBAction) selectReview;


 @end

and this this the detailedViewController.m :

  #import "EntriesDetailedViewController.h"
  #import "Entry.h"
  #import "TheLearningMachineAppDelegate.h"


  @implementation EntriesDetailedViewController

  @synthesize entryTextField1,entryTextField2,textbodyBarButton,managedObjectContext,catLabel, entryNameToolBar,textBodyToolBar, reviewCheck,reviewButton,isChecked,textBackground, entry, undoManager;


  #pragma mark -
  #pragma mark View lifecycle


  - (void)viewWillAppear:(BOOL)animated {

  }

  - (void)viewDidLoad {
      [super viewDidLoad];

  // Configure the title, title bar, and table view.

      self.title = @"Consultation";

    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(save:)]; 
self.navigationItem.rightBarButtonItem = rightButton; 
[rightButton release];

[self setUpUndoManager];

self.entryTextField1.text = self.entry.entryname;
self.entryTextField2.font = [UIFont fontWithName:@"Helvetica" size:17.0];
self.en开发者_Go百科tryTextField2.text = self.entry.textbody;
self.catLabel.title = self.entry.category;
[self.entryTextField2 setFrame:CGRectMake(24, 56, 273, 140)];

self.textBackground.image = [UIImage imageNamed:@"alert_background.png"];
[self.textBackground setFrame:CGRectMake(0,46,312,140)];



  //Initializing a kind of checkBox button

if ([self.entry.active boolValue] == YES) {

    self.reviewCheck.image = [UIImage imageNamed:@"checkedwhitesquare.png"];

    self.isChecked = YES;
}

else {

    self.reviewCheck.image = [UIImage imageNamed:@"UncheckedWhiteSquare.png"];

    self.isChecked = NO;

}

  }


  - (IBAction) selectReview {

      if (isChecked==NO){
        self.reviewCheck.image = [UIImage imageNamed:@"checkedwhitesquare.png"];
    self.isChecked = YES;
    [self.entry setValue:[NSNumber numberWithBool:YES] forKey:@"active"];
    [self performSelector: @selector(save:)];

}
else {

    self.reviewCheck.image = [UIImage imageNamed:@"UncheckedWhiteSquare.png"];

    self.isChecked = NO;
    [self.entry setValue:[NSNumber numberWithBool:YES] forKey:@"active"];
    [self performSelector: @selector(save:)];

}

  }



  - (void)textViewDidBeginEditing:(UITextView  *)entryTextField2{
[self.navigationController setNavigationBarHidden:YES animated:YES];
self.entryNameToolBar.hidden =YES; 
self.textBodyToolBar.hidden =NO;
self.textbodyBarButton.style =UIBarButtonItemStyleBordered;
self.textbodyBarButton.title =@"Terminer la saisie";
[self.entryTextField2 setFrame:CGRectMake(24, 50, 273, 140)];
[self.textBackground setFrame:CGRectMake(0,46,312,140)];
  }

  - (IBAction)textViewDidEndEditing: (UITextView  *)entryTextField2{
      [self.navigationController setNavigationBarHidden:NO animated:YES];
      self.entryNameToolBar.hidden =NO;
      self.textBodyToolBar.hidden =YES;
      [self.entryTextField2 setFrame:CGRectMake(24, 50, 273, 140)];
      [self.textBackground setFrame:CGRectMake(0,46,312,140)];
      self.textbodyBarButton.style =UIBarButtonItemStyleDone;
      self.textbodyBarButton.title =@"Corps de texte";  

  }

  - (IBAction)dismisskeyboard{

[self.entryTextField1 resignFirstResponder];
  }

  - (IBAction)dismissKeyboardfromTextView{

[self.entryTextField2 resignFirstResponder];

  }

  - (void)saveContext {

      NSError *error = nil;
      if (self.managedObjectContext != nil) {
          if ([self.managedObjectContext hasChanges] && ![self.managedObjectContext save:&error]) {

        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Erreur" 
        message:@"blabla" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];


        abort();

          } 
      }
  }  


  - (IBAction)save:(id)sender {

      // if there's text in textfield1 and textfield2 and a category has been set 
      if (([self.entryTextField1.text length]!= 0) && ([self.entryTextField2.text length]!= 0) && ([self.catLabel.title length]!= 0)){ 

    [self.entry setValue:self.entryTextField1.text forKey:@"entryname"];
    [self.entry setValue:self.entryTextField2.text forKey:@"textbody"];
    [self.entry setValue:self.catLabel.title forKey:@"category"];

    if (self.isChecked == YES) {
        [self.entry setValue:[NSNumber numberWithBool:YES]forKey:@"active"];
    }
    else {

        [self.entry setValue:[NSNumber numberWithBool:NO] forKey:@"active"];
    }

    [self.entry setValue:[NSNumber numberWithBool:NO] forKey:@"editable"];

    //save
    NSError *error;
    [self.managedObjectContext save:&error];
    [self saveContext];

    //Return to mainview
    [self.navigationController popViewControllerAnimated:YES];
    [self.entry release];
    //self.entry =nil;
}

else {

    if([self.entryTextField1.text length] ==0){

        UIAlertView *alert =[[UIAlertView alloc] 
                             initWithTitle:@"Oooops..." 
                             message:@"Your entry has no name" 
                             delegate:nil 
                             cancelButtonTitle: @"ok" 
                             otherButtonTitles:nil];
        [alert show];
        [alert release];

    }
    else {

        if ([self.entryTextField2.text length]==0){

            UIAlertView *alert =[[UIAlertView alloc] initWithTitle:@"Oooops..." message:@"Your list is empty" delegate:nil cancelButtonTitle: @"ok" otherButtonTitles:nil];
            [alert show];
            [alert release];

        }
        else {

            if ([self.catLabel.title length]==0){

                UIAlertView *alert =[[UIAlertView alloc] initWithTitle:@"Oooops..." message:@"You need to choose a category for this list first." delegate:nil cancelButtonTitle: @"ok" otherButtonTitles:nil];
                [alert show];
                [alert release];

                  }
              }

          }
      }
   }


  - (void)setEditing {
  //    [super setEditing:editing animated:animated];
  }




  #pragma mark -
  #pragma mark Undo support

  - (void)setUpUndoManager {

    if (self.entry.managedObjectContext.undoManager == nil) {

    NSUndoManager *undoMgr = [[NSUndoManager alloc] init];
    [undoMgr setLevelsOfUndo:1];
    self.entry.managedObjectContext.undoManager = undoMgr;
    [undoMgr release];


    }

    // Register as an observer of the entry's context's undo manager.
    NSUndoManager *entryUndoManager = self.entry.managedObjectContext.undoManager;
NSNotificationCenter *dnc = [NSNotificationCenter defaultCenter];
    [dnc addObserver:self selector:@selector(undoManagerDidUndo:) name:NSUndoManagerDidUndoChangeNotification object:entryUndoManager];
    [dnc addObserver:self selector:@selector(undoManagerDidRedo:) name:NSUndoManagerDidRedoChangeNotification object:entryUndoManager];


  }


  - (void)cleanUpUndoManager {

      // Remove self as an observer.
      [[NSNotificationCenter defaultCenter] removeObserver:self];

      if (self.entry.managedObjectContext.undoManager == undoManager) {
          self.entry.managedObjectContext.undoManager = nil;
          self.undoManager = nil;
      }       
  }


  - (NSUndoManager *)undoManager {
      return self.entry.managedObjectContext.undoManager;
  }


  - (void)undoManagerDidUndo:(NSNotification *)notification {

  }


  - (void)undoManagerDidRedo:(NSNotification *)notification {

  }

  - (BOOL)canBecomeFirstResponder {
      return YES;
  }


  - (void)viewDidAppear:(BOOL)animated {
      [super viewDidAppear:animated];
      [self becomeFirstResponder];
  }


  - (void)viewWillDisappear:(BOOL)animated {
      [super viewWillDisappear:animated];
      [self resignFirstResponder];
  }

- (void)viewDidUnload {

// Release any properties that are loaded in viewDidLoad or can be recreated lazily.


self.entryTextField1 =nil;
self.entryTextField2 = nil;
self.catLabel = nil;
self.entryNameToolBar = nil;
self.textBodyToolBar = nil;
self.reviewCheck = nil;
self.textBackground = nil;
self.reviewButton = nil;
self.entry = nil;
self.navigationItem.rightBarButtonItem = nil;
[self cleanUpUndoManager];
self.entry.managedObjectContext.undoManager = nil;


}

  - (void)dealloc {

[entryTextField1 release], entryTextField1 = nil;
[entryTextField2 release], entryTextField2 = nil;
[catLabel release], catLabel = nil;
[entryNameToolBar release], entryNameToolBar = nil;
[textBodyToolBar release], textBodyToolBar = nil;
[reviewCheck release], reviewCheck = nil;
[textBackground release], textBackground = nil;
[reviewButton release], reviewButton = nil;
[self cleanUpUndoManager];
[entry.managedObjectContext.undoManager release],  
     entry.managedObjectContext.undoManager = nil;
[entry release],entry = nil;
[managedObjectContext release];


[super dealloc];
}

I'm sorry, this is a long bit of code I know but I'm at a loss. Please help me.

As you guys suggested, I did the following modifications :

I've also changed the viewDidUnload and Dealloc portions of the code which now look like this :

I don't quite understand the release + nil concept nor the difference between calling [self.object release] and [object release] in the dealloc and while calling self.object = nil in viewDidUnload.


You should expect your memory usage to go up every time you allocate a new EntriesDetailedViewController. I assume you mean that your memory doesn't go back down when you release it? You're probably not releasing something correctly in -[EntriesDetailedViewController dealloc].

Note that rather than generating a new EntriesDetailedViewController every time, it's probably better here to just create one the first time you need it, and just keep re-configuring it with setEntry:. You've already set up the ivar to hold the view controller; might as well make use of that.


The cause isn't obvious from the code you posted, but Instruments shows you where each allocation took place, so it shouldn't be hard to track it down.


you should use this form in dealloc:

[entryTextField1 release], entryTextField1 = nil;

you should use this form to set (e.g. in viewDidUnload):

self.entryTextField1 = nil;

running static analysis should catch a few more issues. fix them.

once all that is corrected, then re-run the app. (i assume that will not be 100% coverage)

also, make sure you use version control. you will find yourself hunting down a lot of issues - until they are all sorted out, you should expect more issues/crashes along the way.

unfortunately, you can't avoid memory issues forever, and it is painful to track some of them down --while learning proper memory management at the same time (iow, the hard way). good luck!


Hey hey, you know what guys ? I've just found out !!!!!!!! How stupid I am... If you look carefully into the entriesDetailedViewController.h file, you can see that there's a textbodyBarButton that is declared... Well, it was not released, I had forgotten about that one...

So I'm sorry for making you lose your time.

I've just added :

    self.textbodyBarButton = nil;
[textbodyBarButton release];

And I'm fine now, when I go back to the rootViewController, all the memory is released.

So if you guys have the same problem one day, it might be as stupid as that. Just check twice what you have declared in your .h file.

Waouh, 3 days spent on that however...

Big up to you all and thanks again for your patience.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜