你可以帮帮我吗,我的应用程序差不多完成了,我现在正在优化它,所以我正在使用仪器分配工具,我有一个问题vith一个视图控制器,但我无法想象每次我从主视图转到detailedViewController时,为什么内存会增加。
以下是代码部分似乎是问题所在:
- (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];
}
以下是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
这就是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.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时的区别。
答案 0 :(得分:1)
每次分配新的EntriesDetailedViewController
时,您都应该期望内存使用率上升。我猜你的意思是你释放它时你的记忆力不会下降吗?您可能无法在-[EntriesDetailedViewController dealloc]
中正确发布内容。
请注意,不是每次都生成新的EntriesDetailedViewController
,而是在第一次需要时创建一个,而不是仅使用setEntry:
重新配置它。你已经设置了ivar来保存视图控制器;也许可以利用它。
答案 1 :(得分:0)
您发布的代码中的原因并不明显,但是Instruments会向您显示每次分配的位置,因此不应该很难跟踪它。
答案 2 :(得分:0)
你应该在dealloc中使用这个表格:
[entryTextField1 release], entryTextField1 = nil;
您应该使用此表单进行设置(例如在viewDidUnload
中):
self.entryTextField1 = nil;
运行静态分析应该会遇到更多问题。解决它们。
一旦纠正完毕,然后重新运行应用程序。 (我认为不会100%覆盖)
另外,请确保使用版本控制。你会发现自己在追捕很多问题 - 直到它们全部整理完毕,你应该期待更多的问题/崩溃。
不幸的是,你无法永远避免内存问题,追踪其中的一些是很痛苦的 - 同时学习适当的内存管理(哇,艰难的方式)。祝你好运!答案 3 :(得分:0)
所以我很抱歉让你浪费时间。
我刚刚补充道:
self.textbodyBarButton = nil;
[textbodyBarButton release];
现在我很好,当我回到rootViewController时,所有内存都被释放了。
所以,如果你们有一天遇到同样的问题,那可能就像那样愚蠢。只需检查您在.h文件中声明的内容两次。
Waouh,花了3天时间......
非常感谢大家,再次感谢您的耐心等待。