【问题标题】:I want to build app to train people on memory usage (iOS)我想构建应用程序来培训人们的内存使用(iOS)
【发布时间】:2011-10-11 14:16:39
【问题描述】:

我们有很多对 iOS 编程和内存管理相对较新的员工。我想构建一个带有几个显示保留计数的标签和一些按钮来增加和减少这些保留计数的应用程序。

有没有人知道已经有什么可行的方法,或者有任何关于设置它的建议,以便让我的观点得到理解?我有一个工作版本,但它似乎没有按照我认为的方式工作。

ViewController.h

#import <UIKit/UIKit.h>

@interface MemoryTestingViewController : UIViewController {
    UILabel *retainCount;
    UILabel *descLabel;
    UIButton *addRetain;
    UIButton *addRelease;
    UIButton *access;

    NSMutableString *myString;
}

@property (nonatomic, retain) IBOutlet UILabel *retainCount;
@property (nonatomic, retain) IBOutlet UILabel *descLabel;
@property (nonatomic, retain) IBOutlet UIButton *addRetain;
@property (nonatomic, retain) IBOutlet UIButton *addRelease;
@property (nonatomic, retain) IBOutlet UIButton *access;

@property (nonatomic, retain) NSMutableString *myString;

-(IBAction)pressedRetain:(id)sender;
-(IBAction)pressedRelease:(id)sender;
-(IBAction)pressedAccess:(id)sender;

@end



ViewController.m

-(IBAction)pressedAccess:(id)sender {

    descLabel.text = @"Accessing myString, did we crash";
    myString = [NSMutableString stringWithFormat:@"Accessing myString"];

    retainCount.text = [NSString stringWithFormat:@"%i", [myString retainCount]];
}

-(IBAction)pressedRetain:(id)sender {

    descLabel.text = @"Adding 1 to retain count for myString";
    [myString retain];

    retainCount.text = [NSString stringWithFormat:@"%i", [myString retainCount]];
}

-(IBAction)pressedRelease:(id)sender {

    descLabel.text = @"Adding 1 release to myString";
    [myString release];

    retainCount.text = [NSString stringWithFormat:@"%i", [myString retainCount]];

}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    // init our variable string
    myString = [[NSString alloc] init];
    descLabel.text = @"myString retain count after alloc/init";

    // fill our label with myString's retain count starting out
    retainCount.text = [NSString stringWithFormat:@"%i", [myString retainCount]];
    [super viewDidLoad];
}

当它运行时,它看起来很好,但每当我尝试按下保留按钮时就会崩溃。如果有人对如何清理它有任何建议,我将不胜感激。理想情况下,我希望他们在保留计数达到零并且应用程序崩溃时按下访问按钮,但只要保留计数为 1 或更好,访问按钮就应该工作。谢谢。

【问题讨论】:

  • 有趣的事情,试图告诉别人关于内存管理和同时使用retainCount ^^
  • 我意识到使用retainCount 是愚蠢的,但只是试图可视化计数。我可以很好地解释事情,但我认为有一些他们可以操纵的东西会更好。

标签: ios memory-management retain nsautoreleasepool


【解决方案1】:

对象的retainCount 是一件棘手的事情。

如果您要继续沿着这条路走下去,您应该注意以下细节:

  • retainCount 永远不能返回 0
  • 不保证向悬空指针发送消息会崩溃
  • 由于实现细节,一旦您通过任何系统 API 传递对象,就无法知道保留计数
  • 由于实现细节,任何系统类的任何子类都可能具有未知的保留计数
  • 保留计数从不反映对象是否自动释放
  • autoreleases 实际上是线程特定的,而保留计数是线程全局的
  • 某些类有时使用单例实现(NSString,NSNumber 的某些值)
  • 实现细节因平台和版本而异
  • 尝试调配retain/release/autorelease 将不起作用,因为某些类实际上并未使用这些方法来维护保留计数(实现细节、每个平台/版本的更改等)

如果您要教授保留/释放,则应将保留计数视为增量,并完全专注于“如果增加 RC,则必须减少它”。

【讨论】:

  • 感谢您的洞察力。我从来没有打算让应用程序成为主要的培训师,只是为了给人们提供视觉参考以及保留和释放对象的原因/效果。我现在让它以有限的能力工作,这应该足够了。他们仍然需要以艰难的方式学习内存管理的进出。谢谢。
  • 当然——这是个好主意,我鼓励您探索问题空间。了解为什么每个要点都是正确的,可以提供宝贵的知识基础。我应该写一篇博文,每一篇都附上例子。
【解决方案2】:

retainCount 是出了名的不可靠,它返回的值可能非常奇怪。签出this post

【讨论】:

    猜你喜欢
    • 2015-07-26
    • 1970-01-01
    • 2020-07-14
    • 1970-01-01
    • 1970-01-01
    • 2018-11-23
    • 2017-03-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多