Robert J. Clegg
Robert J. Clegg

Reputation: 7360

Whats the difference between these two segments of code?

So, I am wondering whats the difference between these two segments of code - other than just syntax differences?

BBDataStore* tempDataStore = [[BBDataStore alloc] initWithDataExpiry:DATA_EXPIRY_TIME];
    self.dataStore = tempDataStore;

and

self.dataStore = [[BBDataStore alloc] initWithDataExpiry:DATA_EXPIRY_TIME];

Self.dataStore is a property of BBDataStore.

From what I can see - there is no difference here.

In the first example - we eventually assigned tempDataStore to self.dataStore - which is an instance of BBDatastore.

In the second example - we effectively do the same thing... not so?

Upvotes: 0

Views: 92

Answers (3)

user3013314
user3013314

Reputation:

If you are using ARC in your code, both the statements are effectively doing the same (compiler magic happens which ensure both statements end up with same result).

If this were manual memory management with self.dataStore declared as a retain property then,

Code1: Will be ideal way to assign the retain property with an object. This will keep the reference count of the created object to 1 (Your property will be the owner). So when the ivar for the retain property is released in dealloc the memory gets cleared.

BBDataStore* tempDataStore = [[BBDataStore alloc] initWithDataExpiry:DATA_EXPIRY_TIME];
self.dataStore = tempDataStore;
//This will ensure memory does not leak, reference count will get reduced by 1
[tempDataStore release];

Code2: Will have reference count of the object as 2 (One from init and other from retain property). Even after releasing the ivar in dealloc this code will potentially leak memory since the temporary object reference is not available to call release method.

//This created object will have reference count 2
self.dataStore = [[BBDataStore alloc] initWithDataExpiry:DATA_EXPIRY_TIME];
//reference to temporary object is not available hence cannot call release

So in this case for the ARC mechanism the compiler will insert certain lines of code to ensure that memory of your object does not leak (It will create a temporary reference to object, insert release statement on this after assigning to the property, similar to code1).

Hope that helps!

Upvotes: 1

Adnan Aftab
Adnan Aftab

Reputation: 14477

In First case first line creates an object and tempDataStore has reference to it, when second line execute, self.dataStore also point to the same object. hence reference count will increase and two pointers will point to same object. And when tempDataStore scope will end there will be only one reference to the object and that will be self.dataStore. And in second case there is only one pointer reference to object and that is self.datastore

Upvotes: 0

βhargavḯ
βhargavḯ

Reputation: 9836

BBDataStore* tempDataStore = [[BBDataStore alloc] initWithDataExpiry:DATA_EXPIRY_TIME];
self.dataStore = tempDataStore;

This is holding instance of BBDataStore into tempDataStore. And you are assigning tempDataStore to other object say self.dataStore.

self.dataStore = [[BBDataStore alloc] initWithDataExpiry:DATA_EXPIRY_TIME];

This is the direct initialization of self.dataStore.

Difference is only that in first way one more variable come in picture which also consumes memory bit. Why should we consume memory even we have handy approach for initiazation. So you should avoid first way and go with second approach which is very general and appropriate too.

Upvotes: 2

Related Questions