cocoa - RetainCount 内存未释放

标签 cocoa memory-management

我被 Cocoa Memory Managagment 困住了。

- (IBAction) createPush:(UIButton *)sender {
    [create setEnabled:NO];
    [release setEnabled:YES];

    aLocation = [[Location alloc] init];

    // Put some Example Stuff in the Class
    aLocation.title = @"Apartment";
    aLocation.street = @"Examplestreet 23";
    aLocation.zip = @"12345";
    aLocation.city = @"Exampletown";
    aLocation.http = @"http://google.com";
    aLocation.info = @"First Info Text";
    aLocation.info2 = @"Second Info Text, not short as the first one";
    aLocation.logoPath = @"http://google.de/nopic.jpg";

    [aLocation.pictures addObject:@"http://google.de/nopic.jpg"];
    [aLocation.pictures addObject:@"http://google.de/nopic.jpg"];
}

 - (IBAction) releasePush:(UIButton *)sender {
     [release setEnabled:NO];
     [create setEnabled:YES];

     [aLocation release];
     aLocation = nil;
}

如果我设置或获取变量,此代码工作正常,但当我调用“最后”释放时(因此保留计数为0),它会调用aLocation的dealloc方法,但在仪器分配中,您会看到没有给出内存返回。

这里是位置来源:

http://homes.dnsalias.com/sof/Location.m 头文件的相同链接带有“.h”而不是“.m”(抱歉,因为垃圾邮件规则)。

整个项目:http://homes.dnsalias.com/sof/Location.zip

感谢您的帮助,我的失败在哪里?丹尼斯

最佳答案

This Code works fine if I set or get Variables, but when I call the 'last' release (so the retain count is 0) it dealloc Method of aLocation gets called, but in Instruments Allocations you see that no memory is given back.

“没有内存被归还”是什么意思?

尽管名称很奇怪,但上面的代码中 aLocation 的内存管理是正确的(假设您也在 dealloc 中释放了它)。

<小时/>

Why doesn't memory use decrease when a single object is released?

(转述)

您的对象可能相对较小,因此,单次释放低于在 Instruments 中显示所需的约 20K 左右。

如果您的应用由于内存使用问题而崩溃,那么寻求单个释放是错误的起点。首先要做的就是回答为什么您的应用会增加内存以及造成这种增长的原因。

将分配工具配置为仅跟踪实时分配。然后按总内存使用情况排序。这将向您展示哪种类型的分配消耗最多的内存。首先减少它。

Heapshot analysis在这些情况下可能非常有效。

关于cocoa - RetainCount 内存未释放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5886911/

相关文章:

java - 实例方法是按对象还是按类加载到内存中?

objective-c - 未收到通知回复

macos - 如果系统上存在 Mac App Store 版本,则解锁应用程序的开发人员 ID 版本

windows - Visual Studio 2010——如何减少其内存占用

c++ - 引用是存储在堆上还是栈上?

objective-c - 巨大的内存峰值 - CGContextDrawImage

c - 动态内存分配(C 编程)

cocoa - NSCollectionView 与部分 - 就像在 iPhoto 中一样

cocoa - 使用 AFNetworking 通过 POST 上传文件

objective-c - 在 NSObject 中播放 NSSound(使用 Objective-C 的 Mac App)