简体   繁体   中英

iOS - Memory management

i have an array of custom class that is allocated in this way :

myArray = [NSMutableArray array];

I add object in it though a for loop :

for (CustomClass *obj in otherArray) {
    [myArray addObject:obj];
}

Then, i add all to my view :

CustomClass *obj = [myArray objectAtIndex:index];
[self.view addSubview:obj];

Every time i add the element in view i remove it from the array :

[myArray removeObject:obj];

At the end i have all the elements in my view. But, at the end of the game, i have to remove all objects from my view and remove also from memory. I use ARC. I don't know how to release objects also from memory. Thanks

You will have to remove the object from every places it's stocked. ARC will handle for you the retain / release calls (at compile) to fall to zero when unused. You should notice that unreferenced object will no matter what be deallocated.

So you just have to keep in mind where your object is referenced :

Array

When you add it into myArray it adds 1 to retain count.

When you remove it removes 1.

View

When you addSubview it adds 1.

When you want to remove from super view you should call the removeFromSuperview selector on your item (I guess you know it and this isn't your question)

First Array

I noticed it's your objects always stay in the otherArray array.

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