对象保留计数
我像这样分配了一个对象:
PixelInfo *ob1=[[PixelInfo alloc]initWithName:clr :t];
那么对象的保留计数是1。
然后我这样做了......
[faceColor addObject:ob1];
然后保留计数增加到2。为什么?
for(b=xi[i];b<=(xi[i+1]+1);b++)
{
CGPoint t;
t.x=b;
t.y=y;
UIColor *clr=nil;
clr=[self getPixelColorAtLocation:loadImage.CGImage :t];
PixelInfo *ob=[[PixelInfo alloc]initWithName:clr :t];
[faceColor addObject:ob];
[ob release];
}
这是我的代码。即使释放对象ob后,也会发生内存泄漏。为什么?
I allocated an object like this:
PixelInfo *ob1=[[PixelInfo alloc]initWithName:clr :t];
Then the retaincount of object is 1.
Then I did like this....
[faceColor addObject:ob1];
Then the retain count increased to 2. Why?
for(b=xi[i];b<=(xi[i+1]+1);b++)
{
CGPoint t;
t.x=b;
t.y=y;
UIColor *clr=nil;
clr=[self getPixelColorAtLocation:loadImage.CGImage :t];
PixelInfo *ob=[[PixelInfo alloc]initWithName:clr :t];
[faceColor addObject:ob];
[ob release];
}
This is my code .Even after releasing the object ob,Memory leakage happens.Why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
所有集合(Array、Dictionary、Set)都会增加对象的保留计数
[smth addObject: obj]
。附言。 AddSubview 还增加子视图的保留数量
All colllections (Array, Dictionary, Set) increase object's retain count when you doing
[smth addObject: obj]
.PS. AddSubview also increase retain count of subview
我假设faceColor是一个NSMutableArray。当您将对象添加到 NSArray 或 NSDictionary 等容器时,容器会保留添加的对象。这是因为容器现在依赖于添加的对象,只要它位于容器内,它就继续存在。
I assume
faceColor
is an NSMutableArray. When you add an object to a container like NSArray or NSDictionary, the container retains the added object. That's because the container now relies on the added object to continue to exist as long as it is inside the container.在下面的语句中,您已经分配了对象,然后保留计数将增加 1,这就是为什么您得到的是 1。
在下一个语句中,我认为 FaceColor 是一个
NSMutableArray
您正在添加对象 ob1 在一个数组中,所以如果你将它添加到一个数组中,iOS 会将任何对象的保留计数增加 1,
这就是原因,你的保留计数为 2。
编辑:
这里是什么苹果说。
In below statement,you have alloced the object then retain count will be increased by 1 that's why you are getting is 1.
In next statement,I consider faceColor is an
NSMutableArray
You are adding the object ob1 in an array, So iOS will increase the retain count of any object by 1 if you add that in an Array,
That's the reason,You have retain count 2.
EDITED:
Here is what apple says.