0 votes
15 views

What happens if you add your just created object to a mutable array, and you release your object?

in Technology by (189k points)

1 Answer

0 votes

It depends. If you owned the object before adding it to the array, you still must release your own ownership claim to avoid a leak — the array’s ownership claim on the object is separate, and does nothing to absolve you of your own responsibility with respect to the memory management rules.

Object *obj = [[Object alloc] init];

[anArray addObject:obj];

[obj release];

On the other hand, if you did not own the object, then you still don’t own it after you add it to the array, and still must not release it. The array does establish its own ownership claim, but just like your own code, it’s responsible for balancing its own claims.

Object *obj = [Object object];

[anArray addObject:obj];

// or, avoiding a temp variable

[anArray addObject:[Object object]];

by (189k points)

Related questions

0 votes
1 answer 12 views
0 votes
1 answer 14 views
0 votes
1 answer 11 views
11 views asked Nov 10, 2020 in Technology by JackTerrance (189k points)
0 votes
1 answer 28 views
0 votes
1 answer 14 views
0 votes
1 answer 11 views
0 votes
0 answers 10 views
0 votes
1 answer 14 views
14 views asked Nov 10, 2020 in Technology by JackTerrance (189k points)
0 votes
1 answer 11 views
0 votes
1 answer 13 views
...