Reputation: 1697
I have to confirm with something. I am releasing my objects in this way.
[lblTotalQty, lblTotalAmt, imgEmptyBag, lblEmptyBagMsg release];
is this not a proper way?
Please guide me.
Upvotes: 2
Views: 132
Reputation: 16246
Regarding the how, You release objects by sending the release message:
[myObject release];
Never seen the comma-syntax before, and someone suggested only the last object is sent the message (Can anyone confirm this?)
Regarding the when, that's a long story and I recommend you read Apple's docs on the subject; you could start here.
Basically, you must keep track of object ownership to avoid having two parties deallocate the same object twice, or conversely, never.
Sounds cumbersome at first, but it's a very good practice in terms of software design; This ownership hierarchy brings meaningful structure to your code.
Alternatively, IF you are coding for the Mac, you can use Garbage Collection. Also, if you are coding for iOS 5.0+ and are starting a new project, you can take advantage of the new ARC (Automatic Reference Counting) functionality, which is basically Manual Reference Counting code auto-generated by the compiler: The Best of Both Worlds.
Upvotes: 0
Reputation: 44633
Comma operators will discard all expressions except the last so your statement will in effect become
[lblEmptyBagMsg release];
So you shouldn't release objects in the way you've indicated in the question. Do them individually i.e. call release on each object separately.
Upvotes: 6
Reputation: 31642
Its really hard to tell what you're asking here because you give no context.
For one thing, I've never seen anyone use a comma separated list of identifiers to send a single message to, as you do in your example... but I suppose that's not the point of the question.
With the exception of using autorelease pools, sending the release
message to an object is the only way of releasing them.
The confusion with releasing object is usually more a question of "when" not "how". Everyone knows "how" (call release
). The bigger question is usually "when should I be releasing or retaining an object".
Upvotes: 1