Aug 97 - Tips
Volume Number: 13 (1997)
Issue Number: 8
Column Tag: Tips & Tidbits
Tips & Tidbits
by Steve Sisak
Protect Against Dereferencing nil
Dereferencing nil is probably the most common cause of crashes, bus errors, type 11 errors and suchlike. It's very easy to write code like this:
char *myPtr = GetNewCharPtr(256); // allocate a 255-byte string
// put something into your char *.
strcpy(myPtr, "This is a string");
// oops! What happens of the call to GetNewCharPtr() returned
// nil? Your program dies at strcpy! Or--worse--dies 50 lines later!
Most of these errors can be avoided by checking pointers before using them. The following handy routine is something you can us at the beginning of every function to make sure that the pointer parameters passed to the routine are non-nil:
/*-------------------------------------------------------------------------
This handy routine allows you to check several pointer params for nil
with one call. The first parameter to this routine must be the number
of params you want to check. The remaining params are the params to
check, as many as you like.
---------------------------------------------------------------------------*/
void CheckNilParams(long numParams,...)
{
va_list argList;
void *next;
long i;
// walk through each param, up to numParams
va_start(argList, numParams);
for (i = 1; i <= numParams; i++)
{
// get the next param
next = va_arg(argList, void *);
// assert if the param is nul. If you are using c++, you could
// throw an exception here, or do whatever you like. The value of i
// in this loop will tell you which param was nil
assert(next != nil);
}
// we're done with our param list
va_end(argList);
}
Here's how you can use it:
// This function has several pointer params passed to it
void PointerFunc( char *cptr1, void *anotherPtr,
WindowPtr *wptr, int notAPtr)
{
// check each param to make sure its not nil. If any of the above
// params are nil, the program will assert
CheckNilParams(3, cptr1, anotherPtr, wptr);
// now you can safely dereference the pointer params!
}
Since this adds a fair amount of overhead to your code, you'll usually want to use it only in the debug version of your program. It will save you a lot of time in MacsBug!
Vinay Prabhakar
Gray Owl Software
Automatically Generating an Owner Resource
When I was working on my application, I wanted to generate different owner resources depending on some build definitions. I wanted to do this using the rez compiler included with CodeWarrior. You could also do this within ResEdit or Resorcerer, but I like automatic generation far better. Especially if you need to do builds quite often.
Normally your owner resource is of resource type 'the signature', but there is no special definition in one of the .r files located in the interfaces folder. OK, you can use the 'data' type, but then you need to do some serious ascii->hexadecimal conversion.
What I did was the following: create a new type of resource which only contains a pascal string. Of course the type must be the same as your signature (also known as the creator) code.
For example, when your application uses '????' as its signature (don't forget to register your signature along with the filetypes you use by visiting Apple's website at http://gemma.apple.com/dev/cftype), you can use the following in your own .r file:
type '????'
{
pstring;
};
resource '????' (0, "Owner Resource")
{
#ifdef BUILD_DEMO
"Application Demo © 1997 The Software Publishers, Inc"
#else
"Application © 1997 The Software Publishers, Inc"
#endif
};
Note that it is customary to use resource id 0 for your owner resource. Of course you can also define other resource types if you need them for your own data structures you story in the resource fork. You can see some useful examples in the SysTypes.r and Types.r files. It sure beats entering some hexadecimal codes in your text editor!
If you still want to use ResEdit or another editor, you can also consider writing your own resource templates. See chapter 7 in the book "ResEdit Reference, For ResEdit version 2.1" with ISBN 0-201-57767-4 for writing your own resource templates.
Frank Manshande
frankm@and.nl