我一直在開發一個應用程序大約一週左右,現在我認爲這將是一個好時機來分析它,以確保我做的一切正確,什麼是什麼我發現,即使我的所有對象都被分配了,分配量也在增加。當我釋放物體我執行以下操作:對象似乎被釋放,但內存佔用不斷上升
[object release];
object = nil;
在我的應用程序我有確定任一示出了初始視圖控制器我LoginViewController
或TimeLineViewController
,取決於我是否有訪問令牌。 (這部分不重要,因爲我遇到的問題在LoginViewController
/SignupViewController
之內。)。登錄控制器有兩個文本框和兩個按鈕,這些按鈕將sVC推到導航視圖控制器上或嘗試登錄。
奇怪的是,我的視圖和視圖控制器上正在調用dealloc
方法,但內存在調用之後會上升。
SDK 7.0版 的Xcode 5.0版本
編輯:
在我LoginViewController當我從一個LoginView該SignupButton已被點擊的情況下,本方法被稱爲:
- (void)signupButtonPressed
{
SignupViewController *signupVC = [[SignupViewController alloc] init];
[self.navigationController pushViewController:signupVC animated:true];
destroy(signupVC);
}
***注意,破壞宏如下:
#define destroy($x) \
if($x) \
{ \
[$x release]; \
$x = nil; \
}
當SignupViewController創建viewDidLoad方法如下:
self.view = [[SignupView alloc] initWithFrame:self.view.frame];
[[(SignupView *)self.view evtSignupButtonPressed] addHandler:AFFHandler(@selector(signupPressed))];
[((SignupView *)self.view).profileImage addTarget:self action:@selector(profileImagePressed) forControlEvents:UIControlEventTouchUpInside];
[self.navigationController setNavigationBarHidden:false animated:true];
然後創建看起來像這樣對於內部SignupView視圖的用戶界面:
- (void)setupUI
{
self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:IS_IPHONE5 ? @"genericBackground-568h.jpg" : @"genericBackground.jpg"]];
_overlayView = [[UIView alloc] initWithFrame:self.frame];
_scrollView = [[UIScrollView alloc] initWithFrame:self.frame];
_profileImage = [[UIButton alloc] init];
profileImageContainer = [[UIView alloc] initWithFrame:CGRectMake(18.5, 0, _profileImage.imageView.image.size.width + 10, _profileImage.imageView.image.size.height + 10)];
selectProfilePictureText = [[UILabel alloc] initWithFrame:CGRectMake(profileImageContainer.affX, 0, 229, 17)];
UIView *padding = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 17, 40)];
_usernameField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 284, 40)];
_usernameField.delegate = self;
_passwordField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 284, 40)];
_passwordField.delegate = self;
_repeatPasswordField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 284, 40)];
_repeatPasswordField.delegate = self;
_emailField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 284, 40)];
_emailField.delegate = self;
destroy(padding);
buttonImage = [[UIImage imageNamed:@"largeButton.png"] copy];
_submitButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height)];
[_submitButton addTarget:self action:@selector(signupButtonPressed) forControlEvents:UIControlEventTouchUpInside];
destroy(buttonImage);
[self addSubview:_scrollView];
[self addSubview:_overlayView];
[_scrollView addSubview:profileImageContainer];
[profileImageContainer addSubview:_profileImage];
[_scrollView addSubview:selectProfilePictureText];
[_scrollView addSubview:_usernameField];
[_scrollView addSubview:_passwordField];
[_scrollView addSubview:_repeatPasswordField];
[_scrollView addSubview:_emailField];
[_scrollView addSubview:_submitButton];
destroy(profileImageContainer);
destroy(selectProfilePictureText);
}
** A音符,伊夫省略所有改變這些對象屬性的代碼,如改變backgroundColour等等。
的SignupVC和SignupView的dealloc的方法如下:
SignupView:
- (void)dealloc
{
self.usernameField.delegate = nil;
self.passwordField.delegate = nil;
self.repeatPasswordField.delegate = nil;
self.emailField.delegate = nil;
AFFRemoveAllEvents();
destroyAndRemove(_usernameField);
destroyAndRemove(_passwordField);
destroyAndRemove(_repeatPasswordField);
destroyAndRemove(_emailField);
destroyAndRemove(_profileImage);
destroyAndRemove(_submitButton);
destroyAndRemove(_scrollView);
destroyAndRemove(_overlayView);
if(buttonImage)
destroy(buttonImage);
[super dealloc];
}
SignupVC(這被按下導航欄的後退按鈕之後調用)
- (void)dealloc
{
[[(SignupView *)self.view evtSignupButtonPressed] removeHandlersForObserver:self];
[((SignupView *)self.view).profileImage removeTarget:self action:@selector(profileImagePressed) forControlEvents:UIControlEventTouchUpInside];
destroy(profileImage);
destroyAndRemove(self.view);
[super dealloc];
}
DestroyAndRemove是這樣做的:
#define destroyAndRemove($x) \
if($x) \
{ \
[$x removeFromSuperview]; \
[$x release]; \
$x = nil; \
}
對象是否包含對外部資源的任何引用?是基於垃圾收集,參考計數的內存管理方案嗎?不要碰巧知道這個環境。 – joshp
你在看什麼樂器? – RyanR
那是Objective-C的代碼嗎?如果是這樣,請添加一個objective-c標籤。我認爲'[object release]'與C中的'free()'函數類似。如果是這樣,釋放內存使其可以在程序中進一步分配;它並不一定會減少程序佔用的內存大小。 –