RE: [OT] Retain count riddle
RE: [OT] Retain count riddle
- Subject: RE: [OT] Retain count riddle
- From: "M. Uli Kusterer" <email@hidden>
- Date: Wed, 31 Mar 2004 19:05:04 +0200
At 15:31 Uhr -0500 30.03.2004, Jonathan E. Jackel wrote:
This should sorta work:
#define SAFE_SET_PATH(old, new) NSString *temp = [[new lastPathComponent]
retain]; [old release]; old = temp;
Two suggestions:
a) When you create a macro that contains several statements, use
curly brackets to give it its own scope. this avoids namespace
collisions and oddities when using the Macro in a one-line"if"
without curlies.
b) Wherever you use a macro parameter, wrap it in parentheses to
avoid precedence issues if it is an expression. So:
#define SAFE_SET_PATH(old,new) { NSString* temp = [new retain]; [old
release]; old = temp; }
But of course, the smarter venue is always to not use macros for such
stuff and instead use functions. IIRC C99 even brought us inline
functions, which are a much better choice for such cases.
Macros are best only used when there is no other way to achieve
something (e.g. if you need to paste together tokens for identifier
names etc.)
Is it really so difficult to write proper accessors the long way, or use a
tool like Accessorizer? The benefit is that you can instantly debug
problems like this. Do you save enough effort using macros for that
practice to pay off?
You could also just turn on the Script menu in xCode. It contains two
Perl scripts that take the selected variable declaration and create
accessor declarations and definitions from that and put them on the
clipboard.
--
Cheers,
M. Uli Kusterer
------------------------------------------------------------
"The Witnesses of TeachText are everywhere..."
http://www.zathras.de
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.