1

你能帮我吗,我的应用程序快完成了,我现在正在优化它,所以我正在使用 Instrument Allocations 工具,我的视图控制器有问题,但我不知道为什么内存每次我从主视图转到详细视图控制器时都会增加。

这是似乎是问题的代码部分:

  - (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];
}

这是 detailViewController.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

这就是 detailViewController.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.entryTextField2.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];
}

对不起,我知道这是一段很长的代码,但我不知所措。请帮我。

正如你们所建议的,我做了以下修改:

我还更改了代码的 viewDidUnload 和 Dealloc 部分,现在看起来像这样:

我不太了解 release + nil 的概念,也不太了解在 dealloc 中调用 [self.object release] 和 [object release] 以及在 viewDidUnload 中调用 self.object = nil 之间的区别。

4

4 回答 4

1

每次分配新的EntriesDetailedViewController. 我假设你的意思是当你释放它时你的记忆不会回落?您可能没有在-[EntriesDetailedViewController dealloc].

请注意,与其EntriesDetailedViewController每次都生成一个新的,不如在第一次需要时创建一个,然后继续使用setEntry:. 您已经设置了 ivar 来保存视图控制器;不妨利用它。

于 2011-03-17T17:11:39.777 回答
0

从您发布的代码中,原因并不明显,但 Instruments 会向您显示每次分配发生的位置,因此不难找到它。

于 2011-03-17T17:12:01.057 回答
0

你应该在 dealloc 中使用这种形式:

[entryTextField1 release], entryTextField1 = nil;

你应该使用这个表格来设置(例如 in viewDidUnload):

self.entryTextField1 = nil;

运行静态分析应该会发现更多问题。修复它们。

一旦所有这些都得到纠正,然后重新运行该应用程序。(我认为这不会是 100% 的覆盖率)

另外,请确保您使用版本控制。你会发现自己正在寻找很多问题——在它们都被解决之前,你应该期待更多的问题/崩溃。

不幸的是,您无法永远避免内存问题,并且在同时学习正确的内存管理的同时追踪其中一些问题是很痛苦的(iow,艰难的方式)。祝你好运!

于 2011-03-17T20:34:21.083 回答
0

嘿嘿,你们知道吗?我才发现!!!!!!!!!!我是多么愚蠢...如果您仔细查看entryDetailedViewController.h文件,您会看到声明了一个textbodyBarButton...好吧,它没有发布,我忘记了那个...

所以我很抱歉让你浪费了你的时间。

我刚刚添加:

    self.textbodyBarButton = nil;
[textbodyBarButton release];

我现在很好,当我回到 rootViewController 时,所有的内存都被释放了。

因此,如果你们有一天也遇到同样的问题,那可能就这么愚蠢。只需检查两次您在 .h 文件中声明的内容。

哇哦,但是花了3天时间...

感谢你们所有人,再次感谢你们的耐心。

于 2011-03-20T18:33:35.653 回答