调用[myString release]不会减少[myString retainCount] [英] calling [myString release] does NOT decrement [myString retainCount]

查看:68
本文介绍了调用[myString release]不会减少[myString retainCount]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下情况,这似乎导致我的iPad应用程序泄漏内存。

I have the following situation, which seems to cause my iPad application to leak memory.

我有一个带字符串属性的类......

I have a class with a string property...

@property(nonatomic,retain) NSString * synopsis;

我从一些HTTP响应中设置字符串属性,来自JSON或XML响应。

I set the string property from some HTTP response, either from JSON or XML response.

此时,概要对象的保留计数为1.

At that point the retain count of the synopsis object is 1.

但我有这种情况:

我将概要保存到本地sqlite数据库,然后我想从内存中释放它,但我有奇怪的情况,调用 [概要发布] 从我的对象中不会将保留计数减少到0。

I save the synopsis to a local sqlite database, and then I want to release it from memory, but I have the situation where strangely, calling [synopsis release] from within my object does not decrement the retain count to 0.

(void) save
{
  NSLog(@"synopsis before save retainCount=%d",[synopsis retainCount]);
  [self saveToDb:synopsis withKey:@"synopsis"];
  NSLog(@"synopsis after save retainCount=%d",[synopsis retainCount]);
  [synopsis release];
  NSLog(@"synopsis after release retainCount=%d",[synopsis retainCount]);
  synopsis=nil;
}

在控制台中我得到:

synopsis before save retainCount=1

synopsis after save retainCount=1

synopsis after release retainCount=1

这怎么可能?我在模拟器或设备上运行相同的结果。

How can this be possible? I get the same result running in simulator or on the device.

推荐答案

不要依赖 RETAINCOUNT



对于人类来说,它不是对象所有权的准确度量。你不知道框架中幕后的保留发布是什么。

Cocoa中的内存管理很简单:

Memory management in Cocoa is simple:


  1. 如果你 alloc / init 复制一个对象,请确保调用 release 在某些时候。

  2. 如果你想保留一个物体,请拨打保留 - 但要确保在某些时候也要打电话给发布

  1. If you alloc/init or copy an object, make sure you call release on it at some point.
  2. If you want to keep an object around, call retain -- but make sure to call release at some point, too.

这篇关于调用[myString release]不会减少[myString retainCount]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆