TweetFollow Us on Twitter

Feb 96 Tips
Volume Number:12
Issue Number:2
Column Tag:Tips & Tidbits

Tips & Tidbits

By Steve Sisak, Contributing Editor

Note: Source code files accompanying article are located on MacTech CD-ROM or source code disks.

An HGetState Gotcha

HGetState does not return a valid handle state when you pass it an empty handle (one whose master pointer is NULL). Instead, it returns an error code, so before you call HGetState, be sure to check that the handle isn’t empty and execute an alternate code path if it is.

I was bitten by this because I was using HGetState to determine if a handle was to a resource, and the resource had been purged, so HGetState returned an error code instead of the handle flags, and I incorrectly decoded the return result and thought the handle wasn’t to a resource.

Eric Schlegel

From Inside Macintosh:
Memory, page 1-61 to 1-62:

If an error occurs during an attempt to get the state flags of the specified relocatable block, HGetState returns the low-order byte of the result code as its function result. For example, if the handle h points to a master pointer whose value is NIL, then the signed byutle returned by HGetState will contain the value -109.

Result codes:

noErr 0

nilHandleErr -109 NIL master pointer

memWZErr -111 Attempt to operate on a free block

An Old Bug, Turns Out to be “An Old Bug”

I think I found a bug in a Tip in the March 1995 issue of MacTech magazine. The Tip, entitled “Hot Tip for Hot Keys”, can be found on page 67. Allow me to quote a segment of code:

else
{
   num = CountDITL(theDialog);
   for(i=0; i<num; i++)
   {
        GetDItem(theDialog, i, &iType, &iHandle, &iRect);

                  //and so on ... the omitted code works wonderfully!
   }
}

The problem is that the for loop is counting from 0 to Number-Of-DITL-Items minus 1. While the for loop is executed the correct number of times, it’s starting and ending one index too early. This off-by-one error is relatively common in C. Either the for loop should be:

   for (i=1; i<=num; i++)

or the next line should read:

      GetDItem(theDialog, i + 1, &iType, &iHandle, &iRect);

This error is a little hard to detect. Calling GetDItem with itemNo set to 0 doesn’t cause any harm. The only effect this bug has is the final entry in the DITL isn’t included in the search. This final item usually isn’t a push button, so the author didn’t notice the error in his own routine. Otherwise, this code works wonderfully; I use a repaired version all the time!

Mike Trent

A More General Context Sensitive Cursor

In the December issue of MacTech, a technique for making your application WindowShade-savvy was presented, which checked the current content region of the window to see if it was null. A more general method for setting the cursor, which works for WindowShade as well as handling the cases of the cursor outside of the window and when the cursor is in a floating window over the active window is:

   GetMouse(&mouseLoc);
   if (!PtInRgn(mouseLoc,theWindow->visRgn))  
 // theWindow is the current window
   {
                        // Cursor is not in visible region of the window
                  // Set cursor to default arrow.
      InitCursor();
   }
   else
   {
                  // Adjust the cursor to the proper shape
   }

Paul Hyman

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Combo Quest (Games)
Combo Quest 1.0 Device: iOS Universal Category: Games Price: $.99, Version: 1.0 (iTunes) Description: Combo Quest is an epic, time tap role-playing adventure. In this unique masterpiece, you are a knight on a heroic quest to retrieve... | Read more »
Hero Emblems (Games)
Hero Emblems 1.0 Device: iOS Universal Category: Games Price: $2.99, Version: 1.0 (iTunes) Description: ** 25% OFF for a limited time to celebrate the release ** ** Note for iPhone 6 user: If it doesn't run fullscreen on your device... | Read more »
Puzzle Blitz (Games)
Puzzle Blitz 1.0 Device: iOS Universal Category: Games Price: $1.99, Version: 1.0 (iTunes) Description: Puzzle Blitz is a frantic puzzle solving race against the clock! Solve as many puzzles as you can, before time runs out! You have... | Read more »
Sky Patrol (Games)
Sky Patrol 1.0.1 Device: iOS Universal Category: Games Price: $1.99, Version: 1.0.1 (iTunes) Description: 'Strategic Twist On The Classic Shooter Genre' - Indie Game Mag... | Read more »
The Princess Bride - The Official Game...
The Princess Bride - The Official Game 1.1 Device: iOS Universal Category: Games Price: $3.99, Version: 1.1 (iTunes) Description: An epic game based on the beloved classic movie? Inconceivable! Play the world of The Princess Bride... | Read more »
Frozen Synapse (Games)
Frozen Synapse 1.0 Device: iOS iPhone Category: Games Price: $2.99, Version: 1.0 (iTunes) Description: Frozen Synapse is a multi-award-winning tactical game. (Full cross-play with desktop and tablet versions) 9/10 Edge 9/10 Eurogamer... | Read more »
Space Marshals (Games)
Space Marshals 1.0.1 Device: iOS Universal Category: Games Price: $4.99, Version: 1.0.1 (iTunes) Description: ### IMPORTANT ### Please note that iPhone 4 is not supported. Space Marshals is a Sci-fi Wild West adventure taking place... | Read more »
Battle Slimes (Games)
Battle Slimes 1.0 Device: iOS Universal Category: Games Price: $1.99, Version: 1.0 (iTunes) Description: BATTLE SLIMES is a fun local multiplayer game. Control speedy & bouncy slime blobs as you compete with friends and family.... | Read more »
Spectrum - 3D Avenue (Games)
Spectrum - 3D Avenue 1.0 Device: iOS Universal Category: Games Price: $2.99, Version: 1.0 (iTunes) Description: "Spectrum is a pretty cool take on twitchy/reaction-based gameplay with enough complexity and style to stand out from the... | Read more »
Drop Wizard (Games)
Drop Wizard 1.0 Device: iOS Universal Category: Games Price: $1.99, Version: 1.0 (iTunes) Description: Bring back the joy of arcade games! Drop Wizard is an action arcade game where you play as Teo, a wizard on a quest to save his... | Read more »

Price Scanner via MacPrices.net

Apple’s M4 Mac minis on sale for record-low p...
B&H Photo has M4 and M4 Pro Mac minis in stock and on sale right now for up to $150 off Apple’s MSRP, each including free 1-2 day shipping to most US addresses. Prices start at only $469: – M4... Read more
Deal Alert! Mac Studio with M4 Max CPU on sal...
B&H Photo has the standard-configuration Mac Studio model with Apple’s M4 Max CPU in stock today and on sale for $300 off MSRP, now $1699 (10-Core CPU and 32GB RAM/512GB SSD). B&H also... Read more

Jobs Board

All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.