objective-c – 使用UITableView的UIViewController中可能的内存泄漏

objective-c – 使用UITableView的UIViewController中可能的内存泄漏,第1张

概述我有一个UIViewController,它以模态方式呈现.当我观察内存分配Instrument时,显示视图时内存使用量会增加,但是当它退出时,内存不会被释放. 如果我继续打开和关闭视图,内存会不断增加. 仪器不会报告内存泄漏! 可能是什么导致了这个? View Controller代码如下(我跳过了didSelectRow代码). 总是叫Dealloc. 编辑 – 我正在使用ARC .H #im 我有一个UIVIEwController,它以模态方式呈现.当我观察内存分配Instrument时,显示视图时内存使用量会增加,但是当它退出时,内存不会被释放.
如果我继续打开和关闭视图,内存会不断增加.
仪器不会报告内存泄漏!
可能是什么导致了这个? VIEw Controller代码如下(我跳过了dIDSelectRow代码).
总是叫Dealloc.

编辑 – 我正在使用ARC

.H

#import <UIKit/UIKit.h>@class OutlineTextUILabel;@interface StoreVIEwController : UIVIEwController <UItableVIEwDelegate,UItableVIEwDataSource> {    int starCount;    NSMutableArray *_singleUseArray;    NSMutableArray *_fullUseArray;}@property (weak,nonatomic) IBOutlet UItableVIEw *tableVIEw;@property (weak,nonatomic) IBOutlet OutlineTextUILabel *starCountLbl;- (IBAction)exitBtnpressed:(ID)sender;

.M

#import "StoreVIEwController.h"#import "NSUserDefaults+MPSecureUserDefaults.h"#import "PowerUpCell.h"#import "OutlineTextUILabel.h"#import "PowerUpSingleton.h"#import "PowerUp.h"#define kPrefsNumberOfStars             @"numberOfStars"@interface StoreVIEwController ()@end@implementation StoreVIEwController@synthesize tableVIEw = _tableVIEw;@synthesize starCountLbl;#pragma mark VIEw Methods- (voID)vIEwDIDLoad{    [super vIEwDIDLoad];    // display star count    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];    BOol valID = NO;    starCount = [prefs secureIntegerForKey:kPrefsNumberOfStars valID:&valID];    if (!valID) {        NSLog(@"Stars Tampered With!");        self.starCountLbl.text = @"Err";    } else {        self.starCountLbl.text = [Nsstring stringWithFormat:@"%d",starCount];    }    // tablevIEw setup    CGRect frame2 = CGRectMake(0,320,40);    UIVIEw *footer = [[UIVIEw alloc] initWithFrame:frame2];    footer.backgroundcolor = [UIcolor clearcolor];    self.tableVIEw.tableFooterVIEw = footer;    self.tableVIEw.opaque = NO;    self.tableVIEw.backgroundVIEw = nil;}- (voID)vIEwWillAppear:(BOol)animated{    [super vIEwWillAppear:YES];    if (![[PowerUpSingleton sharedList] refreshArray]) {        NSLog(@"Error,%s",__FUNCTION__);    } else {        [self performSelectorOnMainThread:@selector(workOutSingleUsetodisplay) withObject:nil waitUntilDone:YES];        [self performSelectorOnMainThread:@selector(workOutFullUsetodisplay) withObject:nil waitUntilDone:YES];        [self.tableVIEw reloadData];    }}- (voID)workOutSingleUsetodisplay{    _singleUseArray = [[NSMutableArray alloc] init];    for (PowerUp *pu in [[PowerUpSingleton sharedList] sharedArray]) {        if (!pu.fullUnlock) {            [_singleUseArray addobject:pu];        }    }}- (voID)workOutFullUsetodisplay{    _fullUseArray = [[NSMutableArray alloc] init];    for (PowerUp *pu in [[PowerUpSingleton sharedList] sharedArray]) {        if (pu.prefFullname != nil) {            [_fullUseArray addobject:pu];        }    }}- (voID)dIDReceiveMemoryWarning{    [super dIDReceiveMemoryWarning];    // dispose of any resources that can be recreated.}- (BOol)shouldautorotatetoInterfaceOrIEntation:(UIInterfaceOrIEntation)interfaceOrIEntation{    return (interfaceOrIEntation == UIInterfaceOrIEntationPortrait || interfaceOrIEntation == UIInterfaceOrIEntationPortraitUpsIDeDown);}- (voID)vIEwDIDUnload {    [self settableVIEw:nil];    [self setStarCountLbl:nil];    [super vIEwDIDUnload];}#pragma mark tableVIEw Setup Methods- (NSInteger)numberOfSectionsIntableVIEw:(UItableVIEw *)tableVIEw{    return 2;}- (Nsstring *)tableVIEw:(UItableVIEw *)tableVIEw TitleForheaderInSection:(NSInteger)section{    if (section == 0) {        return @"Single Use";    } else if (section == 1) {        return @"Use forever";    }    return nil;}- (NSInteger)tableVIEw:(UItableVIEw *)tableVIEw numberOfRowsInSection:(NSInteger)section{    if (section == 0) {        return [_singleUseArray count];    } else if (section == 1) {        return [_fullUseArray count];    }    return 0;}- (UItableVIEwCell *)tableVIEw:(UItableVIEw *)tableVIEw cellForRowAtIndexPath:(NSIndexPath *)indexPath{    Nsstring *cellIDentifIEr;    if (indexPath.section == 0) {        cellIDentifIEr = @"powerUpCellSingleUse";    } else if (indexPath.section == 1) {        cellIDentifIEr = @"powerUpCell";    }    PowerUpCell *cell = [tableVIEw dequeueReusableCellWithIDentifIEr:cellIDentifIEr];    if (cell == nil) {        cell = [[PowerUpCell alloc] initWithStyle:UItableVIEwCellStyleDefault reuseIDentifIEr:cellIDentifIEr];    }    if (indexPath.section == 0) {        PowerUp *tmpPU = [_singleUseArray objectAtIndex:indexPath.row];        cell.descriptionLbl.text = tmpPU.displayname;        int cost = tmpPU.costSingle;        cell.costLbl.text = [Nsstring stringWithFormat:@"%d",cost];        if (cost > starCount) {            cell.costLbl.textcolor = [UIcolor redcolor];        } else {            cell.costLbl.textcolor = [UIcolor bluecolor];        }        int howMany = tmpPU.numberOwned;        cell.howManyLbl.text = [Nsstring stringWithFormat:@"%d",howMany];    } else if (indexPath.section == 1) {        PowerUp *tmpPU = [_fullUseArray objectAtIndex:indexPath.row];        cell.descriptionLbl.text = tmpPU.displayname;        int cost = tmpPU.costFull;        cell.costLbl.text = [Nsstring stringWithFormat:@"%d",cost];        if (cost > starCount) {            cell.costLbl.textcolor = [UIcolor redcolor];        } else {            cell.costLbl.textcolor = [UIcolor bluecolor];        }        if (tmpPU.fullUnlock) {            cell.costLbl.textcolor = [UIcolor greencolor];            cell.costLbl.text = @"---";        }    }    return cell;}#pragma mark -- (IBAction)exitBtnpressed:(ID)sender{    [self dismissModalVIEwControllerAnimated:YES];}- (voID)dealloc{    NSLog(@"%s",__FUNCTION__);    self.tableVIEw = nil;    self.starCountLbl = nil;}@end

编辑————-
似乎有点不对劲.我已经向单元格alloc添加了一个NSLog,即使创建了单元格,它也从未被调用过!

PowerUpCell *cell = [tableVIEw dequeueReusableCellWithIDentifIEr:cellIDentifIEr];    if (cell == nil) {        NSLog(@"new cell");        cell = [[PowerUpCell alloc] initWithStyle:UItableVIEwCellStyleDefault reuseIDentifIEr:cellIDentifIEr];    }

编辑7月1日——
我添加了一个导航控制器,现在使用push而不是modal,这个问题仍然存在.
我通过在视图之间前后移动几次来使用乐器拍摄镜头,看起来可能是单元格仍然悬空,因为此屏幕截图显示手势识别器仍然在前一次加载视图时.

解决方法 这是因为你使用IBOutlets弱,而不是使用强.

我实际上认为这是XCode环境中的一个缺陷,因为它应该警告你这种行为.

作为一种最佳实践,我建议让XCode通过将视图拖动到Interface Builder中的代码来生成IBOutlets,以避免这种烦人的陷阱.

总结

以上是内存溢出为你收集整理的objective-c – 使用UITableView的UIViewController中可能的内存泄漏全部内容,希望文章能够帮你解决objective-c – 使用UITableView的UIViewController中可能的内存泄漏所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1021456.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-23
下一篇2022-05-23

发表评论

登录后才能评论

评论列表(0条)

    保存