Question

if i did this

Object * myObject = [[Object alloc]init];
[myObject release];

is there anything wrong about allocating my object in next line

myObject = [[Object alloc]init];

again?

Was it helpful?

Solution

No problem at all. [myobject release]; releases the object pointed at by myObject.

Later, myobject = [[Object alloc] init] will make myobject point to another object.

OTHER TIPS

This is safe to do.

The reason is that myObject is not an object, it's a reference (or pointer if you want to be exact) to the object. That means you've got 2 completely independent objects, but you forget about the reference to the first.

There is nothing wrong with that. That is how you make sure you don't leak your first object.

However, you are not technically allocating the released object again. You are just using the old pointer again.

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

will result in leaking the first object you created.

yes of course. this technique is specially useful in local method variables where you can reuse the object declared once by reallocating it again as new object..!!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top