iPhone 核心数据 - 获取的托管对象未在设备上释放(在模拟器上正常)

发布于 2024-11-13 11:03:58 字数 6488 浏览 4 评论 0原文

我目前正在努力解决我的应用程序的核心数据问题,该问题违反了(我的)逻辑。我确信我做错了什么,但看不出是什么。

我正在我的核心数据实体上执行基本的executeFetchRequest,但是返回的托管对象数组似乎永远不会被释放,只有当我在iPhone上运行它时,它在模拟器下完全按预期工作。

尽管使用 NSAutoreleasePool 来确保内存占用最小化,但还是如此。我还检查了 Instruments,没有泄漏,只是不断增加内存分配(通过“[NSManagedObject(_PFDynamicAccessorsAndPropertySupport) allocWithEntity:]”)。在我的实际应用程序中,这最终会导致 didReceiveMemoryWarning 调用。

我制作了一个最小的程序来重现下面的问题。我尝试过各种方法,例如在排空池子之前对所有对象进行故障排除,但没有任何乐趣。如果我提供一个指向 fetch 的 NSError 指针,则不会返回错误。没有后台线程在运行。

#import <mach/mach.h>
#import <mach/mach_host.h>

+(natural_t) get_free_memory {
    mach_port_t host_port;
    mach_msg_type_number_t host_size;
    vm_size_t pagesize;
    host_port = mach_host_self();
    host_size = sizeof(vm_statistics_data_t) / sizeof(integer_t);
    host_page_size(host_port, &pagesize);
    vm_statistics_data_t vm_stat;
    if (host_statistics(host_port, HOST_VM_INFO, (host_info_t)&vm_stat, &host_size) != KERN_SUCCESS) {
        NSLog(@"Failed to fetch vm statistics");
        return 0;
    }
    /* Stats in bytes */
    natural_t mem_free = vm_stat.free_count * pagesize;
    return mem_free;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Set up the edit and add buttons.
    self.navigationItem.leftBarButtonItem = self.editButtonItem;

    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject)];
    self.navigationItem.rightBarButtonItem = addButton;
    [addButton release];

    // Obtain the Managed Object Context
    NSManagedObjectContext *context = [(id)[[UIApplication sharedApplication] delegate] managedObjectContext];

    // Check the free memory before we start
    NSLog(@"INITIAL FREEMEM: %d", [RootViewController get_free_memory]);

    // Loop around a few times
    for(int i=0; i<20; i++) {

        // Create an autorelease pool just for this loop
        NSAutoreleasePool *looppool = [[NSAutoreleasePool alloc] init];

        // Check the free memory each time around the loop
        NSLog(@"FREEMEM: %d", [RootViewController get_free_memory]);

        // Create a minimal request
        NSEntityDescription *entityDescription = [NSEntityDescription                                                  
                                              entityForName:@"TestEntity" inManagedObjectContext:context];        
        NSFetchRequest *request = [[NSFetchRequest alloc] init];
        [request setEntity:entityDescription];

        // Perform the fetch
        NSArray *array = [context executeFetchRequest:request error:nil];        
        [request release];

        // Drain the pool - should release the fetched managed objects?
        [looppool drain];

    }

    // Check the free menory at the end
    NSLog(@"FINAL FREEMEM: %d", [RootViewController get_free_memory]);

}

当我在模拟器上运行上述内容时,我得到以下输出(这对我来说看起来很合理):

2011-06-06 09:50:28.123 renniksoft[937:207] INITIAL FREEMEM: 14782464
2011-06-06 09:50:28.128 renniksoft[937:207] FREEMEM: 14807040
2011-06-06 09:50:28.135 renniksoft[937:207] FREEMEM: 14831616
2011-06-06 09:50:28.139 renniksoft[937:207] FREEMEM: 14852096
2011-06-06 09:50:28.142 renniksoft[937:207] FREEMEM: 14872576
2011-06-06 09:50:28.146 renniksoft[937:207] FREEMEM: 14897152
2011-06-06 09:50:28.149 renniksoft[937:207] FREEMEM: 14917632
2011-06-06 09:50:28.153 renniksoft[937:207] FREEMEM: 14938112
2011-06-06 09:50:28.158 renniksoft[937:207] FREEMEM: 14962688
2011-06-06 09:50:28.161 renniksoft[937:207] FREEMEM: 14983168
2011-06-06 09:50:28.165 renniksoft[937:207] FREEMEM: 14741504
2011-06-06 09:50:28.168 renniksoft[937:207] FREEMEM: 14770176
2011-06-06 09:50:28.174 renniksoft[937:207] FREEMEM: 14790656
2011-06-06 09:50:28.177 renniksoft[937:207] FREEMEM: 14811136
2011-06-06 09:50:28.182 renniksoft[937:207] FREEMEM: 14831616
2011-06-06 09:50:28.186 renniksoft[937:207] FREEMEM: 14589952
2011-06-06 09:50:28.189 renniksoft[937:207] FREEMEM: 14610432
2011-06-06 09:50:28.192 renniksoft[937:207] FREEMEM: 14630912
2011-06-06 09:50:28.194 renniksoft[937:207] FREEMEM: 14651392
2011-06-06 09:50:28.197 renniksoft[937:207] FREEMEM: 14671872
2011-06-06 09:50:28.200 renniksoft[937:207] FREEMEM: 14692352
2011-06-06 09:50:28.203 renniksoft[937:207] FINAL FREEMEM: 14716928

但是,当我在实际的 iPhone 4 (4.3.3) 上运行它时,我得到以下结果:

2011-06-06 09:55:54.341 renniksoft[4727:707] INITIAL FREEMEM: 267927552
2011-06-06 09:55:54.348 renniksoft[4727:707] FREEMEM: 267952128
2011-06-06 09:55:54.702 renniksoft[4727:707] FREEMEM: 265818112
2011-06-06 09:55:55.214 renniksoft[4727:707] FREEMEM: 265355264
2011-06-06 09:55:55.714 renniksoft[4727:707] FREEMEM: 264892416
2011-06-06 09:55:56.215 renniksoft[4727:707] FREEMEM: 264441856
2011-06-06 09:55:56.713 renniksoft[4727:707] FREEMEM: 263979008
2011-06-06 09:55:57.226 renniksoft[4727:707] FREEMEM: 264089600
2011-06-06 09:55:57.721 renniksoft[4727:707] FREEMEM: 263630848
2011-06-06 09:55:58.226 renniksoft[4727:707] FREEMEM: 263168000
2011-06-06 09:55:58.726 renniksoft[4727:707] FREEMEM: 262705152
2011-06-06 09:55:59.242 renniksoft[4727:707] FREEMEM: 262852608
2011-06-06 09:55:59.737 renniksoft[4727:707] FREEMEM: 262389760
2011-06-06 09:56:00.243 renniksoft[4727:707] FREEMEM: 261931008
2011-06-06 09:56:00.751 renniksoft[4727:707] FREEMEM: 261992448
2011-06-06 09:56:01.280 renniksoft[4727:707] FREEMEM: 261574656
2011-06-06 09:56:01.774 renniksoft[4727:707] FREEMEM: 261148672
2011-06-06 09:56:02.290 renniksoft[4727:707] FREEMEM: 260755456
2011-06-06 09:56:02.820 renniksoft[4727:707] FREEMEM: 260837376
2011-06-06 09:56:03.334 renniksoft[4727:707] FREEMEM: 260395008
2011-06-06 09:56:03.825 renniksoft[4727:707] FREEMEM: 259932160
2011-06-06 09:56:04.346 renniksoft[4727:707] FINAL FREEMEM: 259555328

可用内存量减少了每个循环时间与我获取的托管对象成比例,例如,如果我获取两倍的对象,则可用内存会以两倍的速度减少 - 所以我非常有信心管理对象没有被释放。

请注意,正在获取的实体非常基本,只有两个属性:一个字符串和一个 16 位整数。上面的示例中提取了 1000 个。我用来生成它们的代码如下:

// Create test entities
for(int i=0; i<1000; i++) {
    id entity = [NSEntityDescription insertNewObjectForEntityForName:@"TestEntity" inManagedObjectContext:context];
    [entity setValue:[NSString stringWithFormat:@"%d",i] forKey:@"name"];
    [entity setValue:[NSNumber numberWithInt:i] forKey:@"value"];
}
if (![context save:nil]) {
    NSLog(@"Couldn't save");
}

如果有人可以向我解释发生了什么,我将非常感激!这个问题是唯一阻碍我的应用程序发布的问题。它在模拟器上运行得很好!

如果我可以提供更多信息,请告诉我。

I'm currently struggling with a core data issue with my app that defies (my) logic. I'm sure I'm doing something wrong but can't see what.

I am doing a basic executeFetchRequest on my core data entity, but the array of managed objects returned never seems to be released ONLY when I run it on the iPhone, under the simulator it works exactly as expected.

This is despite using an NSAutoreleasePool to ensure the memory footprint is minimised. I have also checked with Instruments and there are no leaks, just ever increasing allocations of memory (by '[NSManagedObject(_PFDynamicAccessorsAndPropertySupport) allocWithEntity:]'). In my actual app this eventually leads to a didReceiveMemoryWarning call.

I have produced a minimal program that reproduces the problem below. I have tried various things such as faulting all the objects before draining the pool, but with no joy. If I provide an NSError pointer to the fetch no error is returned. There are no background threads running.

#import <mach/mach.h>
#import <mach/mach_host.h>

+(natural_t) get_free_memory {
    mach_port_t host_port;
    mach_msg_type_number_t host_size;
    vm_size_t pagesize;
    host_port = mach_host_self();
    host_size = sizeof(vm_statistics_data_t) / sizeof(integer_t);
    host_page_size(host_port, &pagesize);
    vm_statistics_data_t vm_stat;
    if (host_statistics(host_port, HOST_VM_INFO, (host_info_t)&vm_stat, &host_size) != KERN_SUCCESS) {
        NSLog(@"Failed to fetch vm statistics");
        return 0;
    }
    /* Stats in bytes */
    natural_t mem_free = vm_stat.free_count * pagesize;
    return mem_free;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Set up the edit and add buttons.
    self.navigationItem.leftBarButtonItem = self.editButtonItem;

    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject)];
    self.navigationItem.rightBarButtonItem = addButton;
    [addButton release];

    // Obtain the Managed Object Context
    NSManagedObjectContext *context = [(id)[[UIApplication sharedApplication] delegate] managedObjectContext];

    // Check the free memory before we start
    NSLog(@"INITIAL FREEMEM: %d", [RootViewController get_free_memory]);

    // Loop around a few times
    for(int i=0; i<20; i++) {

        // Create an autorelease pool just for this loop
        NSAutoreleasePool *looppool = [[NSAutoreleasePool alloc] init];

        // Check the free memory each time around the loop
        NSLog(@"FREEMEM: %d", [RootViewController get_free_memory]);

        // Create a minimal request
        NSEntityDescription *entityDescription = [NSEntityDescription                                                  
                                              entityForName:@"TestEntity" inManagedObjectContext:context];        
        NSFetchRequest *request = [[NSFetchRequest alloc] init];
        [request setEntity:entityDescription];

        // Perform the fetch
        NSArray *array = [context executeFetchRequest:request error:nil];        
        [request release];

        // Drain the pool - should release the fetched managed objects?
        [looppool drain];

    }

    // Check the free menory at the end
    NSLog(@"FINAL FREEMEM: %d", [RootViewController get_free_memory]);

}

When I run the above on the simulator I get the following output (which looks reasonable to me):

2011-06-06 09:50:28.123 renniksoft[937:207] INITIAL FREEMEM: 14782464
2011-06-06 09:50:28.128 renniksoft[937:207] FREEMEM: 14807040
2011-06-06 09:50:28.135 renniksoft[937:207] FREEMEM: 14831616
2011-06-06 09:50:28.139 renniksoft[937:207] FREEMEM: 14852096
2011-06-06 09:50:28.142 renniksoft[937:207] FREEMEM: 14872576
2011-06-06 09:50:28.146 renniksoft[937:207] FREEMEM: 14897152
2011-06-06 09:50:28.149 renniksoft[937:207] FREEMEM: 14917632
2011-06-06 09:50:28.153 renniksoft[937:207] FREEMEM: 14938112
2011-06-06 09:50:28.158 renniksoft[937:207] FREEMEM: 14962688
2011-06-06 09:50:28.161 renniksoft[937:207] FREEMEM: 14983168
2011-06-06 09:50:28.165 renniksoft[937:207] FREEMEM: 14741504
2011-06-06 09:50:28.168 renniksoft[937:207] FREEMEM: 14770176
2011-06-06 09:50:28.174 renniksoft[937:207] FREEMEM: 14790656
2011-06-06 09:50:28.177 renniksoft[937:207] FREEMEM: 14811136
2011-06-06 09:50:28.182 renniksoft[937:207] FREEMEM: 14831616
2011-06-06 09:50:28.186 renniksoft[937:207] FREEMEM: 14589952
2011-06-06 09:50:28.189 renniksoft[937:207] FREEMEM: 14610432
2011-06-06 09:50:28.192 renniksoft[937:207] FREEMEM: 14630912
2011-06-06 09:50:28.194 renniksoft[937:207] FREEMEM: 14651392
2011-06-06 09:50:28.197 renniksoft[937:207] FREEMEM: 14671872
2011-06-06 09:50:28.200 renniksoft[937:207] FREEMEM: 14692352
2011-06-06 09:50:28.203 renniksoft[937:207] FINAL FREEMEM: 14716928

However, when I run it on an actual iPhone 4 (4.3.3) I get the following result:

2011-06-06 09:55:54.341 renniksoft[4727:707] INITIAL FREEMEM: 267927552
2011-06-06 09:55:54.348 renniksoft[4727:707] FREEMEM: 267952128
2011-06-06 09:55:54.702 renniksoft[4727:707] FREEMEM: 265818112
2011-06-06 09:55:55.214 renniksoft[4727:707] FREEMEM: 265355264
2011-06-06 09:55:55.714 renniksoft[4727:707] FREEMEM: 264892416
2011-06-06 09:55:56.215 renniksoft[4727:707] FREEMEM: 264441856
2011-06-06 09:55:56.713 renniksoft[4727:707] FREEMEM: 263979008
2011-06-06 09:55:57.226 renniksoft[4727:707] FREEMEM: 264089600
2011-06-06 09:55:57.721 renniksoft[4727:707] FREEMEM: 263630848
2011-06-06 09:55:58.226 renniksoft[4727:707] FREEMEM: 263168000
2011-06-06 09:55:58.726 renniksoft[4727:707] FREEMEM: 262705152
2011-06-06 09:55:59.242 renniksoft[4727:707] FREEMEM: 262852608
2011-06-06 09:55:59.737 renniksoft[4727:707] FREEMEM: 262389760
2011-06-06 09:56:00.243 renniksoft[4727:707] FREEMEM: 261931008
2011-06-06 09:56:00.751 renniksoft[4727:707] FREEMEM: 261992448
2011-06-06 09:56:01.280 renniksoft[4727:707] FREEMEM: 261574656
2011-06-06 09:56:01.774 renniksoft[4727:707] FREEMEM: 261148672
2011-06-06 09:56:02.290 renniksoft[4727:707] FREEMEM: 260755456
2011-06-06 09:56:02.820 renniksoft[4727:707] FREEMEM: 260837376
2011-06-06 09:56:03.334 renniksoft[4727:707] FREEMEM: 260395008
2011-06-06 09:56:03.825 renniksoft[4727:707] FREEMEM: 259932160
2011-06-06 09:56:04.346 renniksoft[4727:707] FINAL FREEMEM: 259555328

The amount of free memory reduces each time round the loop in proportion to the managed objects I fetch e.g. if I fetch twice as many objects then the free memory reduces twice as quickly - so I'm pretty confident it is the managed objects that are not being released.

Note that the entities that are being fetched are very basic, just two attributes, a string and a 16 bit integer. There are 1000 of them being fetched in the examples above. The code I used to generate them is as follows:

// Create test entities
for(int i=0; i<1000; i++) {
    id entity = [NSEntityDescription insertNewObjectForEntityForName:@"TestEntity" inManagedObjectContext:context];
    [entity setValue:[NSString stringWithFormat:@"%d",i] forKey:@"name"];
    [entity setValue:[NSNumber numberWithInt:i] forKey:@"value"];
}
if (![context save:nil]) {
    NSLog(@"Couldn't save");
}

If anyone can explain to me what is going on I'd be very grateful! This issue is the only only one holding up the release of my app. It works beautifully on the simulator!!

Please let me know if there's any more info I can supply.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

So尛奶瓶 2024-11-20 11:03:58

哎哟!这就是我看起来愚蠢的地方!

我将“NSZombieEnabled”设置为 YES,这意味着获取的数据没有被释放!没有这个一切都工作正常:-)

Doh! Here's the point where I look dumb!

I had 'NSZombieEnabled' set to YES which meant that the fetched data wasn't getting released! It's all working fine without that :-)

终难遇 2024-11-20 11:03:58

我现在没有可以测试的 MacOs 设备。但我发现您没有注册 NSFetchRequest 进行自动释放。

我会尝试这种方法:

NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];

I don't have a MacOs device were I can test now. But I see you are not registering NSFetchRequest for autorelease.

I would try to this approach:

NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文