views:

64

answers:

3

if I release a variable more than it should be, would that be a problem? I cannot determine the retain count ahead of time.

+1  A: 

Don't worry about the retainCount. Only send release to an object that you own. If you get an object by sending alloc to a class, or send an object a retain, copy or mutableCopy message, then you own the object and are responsible for sending it a release message at some time in the future.

It's all explained in the Memory Management Guide.

Abizern
let's say i have one alloc but released twice. would that be a problem?
Yazzmi
You never release twice for an object that you alloc once. The `retainCount` won't always match how many times you alloc'd it since there are things going on in the OS background that might retain the object for its own use. **Only** release it once if you retained or alloc'd it once.
iWasRobbed
Yes, that would be a problem. It may not cause a crash straight away, but it will cause a crash sometime in the future and will be very tricky to track down if you do it inadvertently.
Jasarien
I think the question is; why would you release it a second time?
Abizern
it might be released a second time. depending on how the user uses it
Yazzmi
If you have a code path where an object may be released a second time, then you're doing something wrong.
Abizern
`init` doesn't increase a retain count, only `alloc` does, otherwise `[[Class alloc] init]` pairs would have +2 retain count.
dreamlax
so if I init an object multiple times. I only need to release it once?
Yazzmi
@Yazzmi: In theory yes, but, you should never `init` an object more than once. I was only stating that it is `alloc` that causes the +1, not `init`.
dreamlax
+2  A: 
dreamlax
A: 

Please please please do not over release an object ever.

While you're developing your app, it's wise to set the environment variable

MallocScribble="YES"

This will immediately overwrite the pointer for a free'd object to 0x555 so you'll crash where you wouldn't before. Also break on obj-c exceptions! These two settings enabled will help make your app more robust.

jarjar
MallocScribble and what's the other variable? These are Xcode settings right? and make the debugger more sensitive?
Yazzmi