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
2010-08-09 00:22:57
let's say i have one alloc but released twice. would that be a problem?
Yazzmi
2010-08-09 00:25:10
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
2010-08-09 00:28:23
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
2010-08-09 00:28:36
I think the question is; why would you release it a second time?
Abizern
2010-08-09 00:31:07
it might be released a second time. depending on how the user uses it
Yazzmi
2010-08-09 01:42:18
If you have a code path where an object may be released a second time, then you're doing something wrong.
Abizern
2010-08-09 04:38:38
`init` doesn't increase a retain count, only `alloc` does, otherwise `[[Class alloc] init]` pairs would have +2 retain count.
dreamlax
2010-08-09 04:41:45
so if I init an object multiple times. I only need to release it once?
Yazzmi
2010-08-09 06:26:20
@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
2010-08-09 07:48:03
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
2010-08-09 04:06:11
MallocScribble and what's the other variable? These are Xcode settings right? and make the debugger more sensitive?
Yazzmi
2010-08-09 06:19:04