gpt4 book ai didi

ios - 废弃的内存问题

转载 作者:行者123 更新时间:2023-11-29 13:49:49 25 4
gpt4 key购买 nike

你能帮帮我吗,我的应用程序快完成了,我现在正在优化它,所以我正在使用 Instrument Allocations 工具,我在 View Controller 上遇到了问题,但我想不通了解为什么每次从主视图转到 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 之间的区别。

最佳答案

每次您分配一个新的 EntriesDetailedViewController 时,您应该预料到您的内存使用量会增加。我假设你的意思是当你释放它时你的内存不会下降?您可能没有在 -[EntriesDetailedViewController dealloc] 中正确发布某些内容。

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

关于ios - 废弃的内存问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5342321/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com