• MacTech Network:
  • Tech Support
  • |
  • MacForge.net
  • |
  • Apple News
  • |
  • Register Domains
  • |
  • SSL Certificates
  • |
  • iPod Deals
  • |
  • Mac Deals
  • |
  • Mac Book Shelf

MAC TECH

  • Home
  • Magazine
    • About MacTech in Print
    • Issue Table of Contents
    • Subscribe
    • Risk Free Sample
    • Back Issues
    • MacTech DVD
  • Archives
    • MacTech Print Archives
    • MacMod
    • MacTutor
    • FrameWorks
    • develop
  • Forums
  • News
    • MacTech News
    • MacTech Blog
    • MacTech Reviews and KoolTools
    • Whitepapers, Screencasts, Videos and Books
    • News Scanner
    • Rumors Scanner
    • Documentation Scanner
    • Submit News or PR
    • MacTech News List
  • Store
  • Apple Expo
    • by Category
    • by Company
    • by Product
  • Job Board
  • Editorial
    • Submit News or PR
    • Writer's Kit
    • Editorial Staff
    • Editorial Calendar
  • Advertising
    • Benefits of MacTech
    • Mechanicals and Submission
    • Dates and Deadlines
    • Submit Apple Expo Entry
  • User
    • Register for Ongoing Raffles
    • Register new user
    • Edit User Settings
    • Logout
  • Contact
    • Customer Service
    • Webmaster Feedback
    • Submit News or PR
    • Suggest an article
  • Connect Tools
    • MacTech Live Podcast
    • RSS Feeds
    • Twitter

ADVERTISEMENT

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

 
MacTech Only Search:
Community Search:

 
 
 

 
 
 
 
 
  • SPREAD THE WORD:
  • Slashdot
  • Digg
  • Del.icio.us
  • Reddit
  • Newsvine
  • Generate a short URL for this page:



MacTech Magazine. www.mactech.com
Toll Free 877-MACTECH, Outside US/Canada: 805-494-9797
MacTech is a registered trademark of Xplain Corporation. Xplain, "The journal of Apple technology", Apple Expo, Explain It, MacDev, MacDev-1, THINK Reference, NetProfessional, Apple Expo, MacTech Central, MacTech Domains, MacNews, MacForge, and the MacTutorMan are trademarks or service marks of Xplain Corporation. Sprocket is a registered trademark of eSprocket Corporation. Other trademarks and copyrights appearing in this printing or software remain the property of their respective holders.
All contents are Copyright 1984-2010 by Xplain Corporation. All rights reserved. Theme designed by Icreon.
 
Nov. 20: Take Control of Syncing Data in Sow Leopard' released
Nov. 19: Cocktail 4.5 (Leopard Edition) released
Nov. 19: macProVideo offers new Cubase tutorials
Nov. 18: S Stardom anounces Safe Capsule, a companion piece for Apple's
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live