0

can this code cause any potential trouble?

@property (nonatomic, retain) NSDictionary *instanceDictionary;

for(int i = 0; i < 50; i++){
   self.instanceDictionary = [NSDictionary alloc] init];
}

or without self

for(int i = 0; i < 50; i++){
   instanceDictionary = [NSDictionary alloc] init];
}

I came across situations where a instance variable gets "overridden" like this and was wondering if it could cause any memory problems.

1
  • you re assigning a new reference to the same variable each time, and that reference gets overwritten. Commented Aug 4, 2015 at 14:34

1 Answer 1

3

It won't cause any "memory problem". In your code, at each loop iteration, the instanceDictionary is replaced with a new one.
ARC will take care of releasing the previous one automatically.
This code is pretty useless though.

Sign up to request clarification or add additional context in comments.

3 Comments

It should be noted that in the first example, the memory wouldn't be a problem under MRC but the 2nd approach would be a huge leak under MRC. Both are fine (but pointless) under ARC.
I don´t understand why would the second example be a huge leak under MRC?
Because you assign memory and immediately forget the pointer to it, so you won't be able to release it manually.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.