简体   繁体   中英

Object retain count

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?

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

PS. AddSubview also increase retain count of subview

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM