开发者

Object retain count

开发者 https://www.devze.com 2023-03-01 12:26 出处:网络
I allocated an object like this: PixelInfo *ob1=[[PixelInfo alloc]initWithName:clr :t]; Then the retaincount of object is 1.

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....

[fa开发者_运维知识库ceColor 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?


All colllections (Array, Dictionary, Set) increase object's retain count when you doing [smth addObject: obj].

PS. AddSubview also increase retain count of subview


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.


In below statement,you have alloced the object then retain count will be increased by 1 that's why you are getting is 1.

PixelInfo *ob1=[[PixelInfo alloc]initWithName:clr :t];

In next statement,I consider faceColor is an NSMutableArray

[faceColor addObject:ob1];

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.

Important: This method is typically of no value in debugging memory management issues. Because any number of framework objects may have retained an object in order to hold references to it, while at the same time autorelease pools may be holding any number of deferred releases on an object, it is very unlikely that you can get useful information from this method.

0

精彩评论

暂无评论...
验证码 换一张
取 消