May 95 Tips
Volume Number: | | 11
|
Issue Number: | | 5
|
Column Tag: | | Tips & Tidbits
|
Tips & Tidbits
By Scott T Boyd, Editor
Note: Source code files accompanying article are located on MacTech CD-ROM or source code disks.
TIP OF THE MONTH
Flush Out Your Bugs
Before releasing an application to your testers, load these three extensions: EBBE (Even Better Bus Error), DisposeResource, and DoubleTrouble. They check for NIL dereferences, improperly-disposed resources, and multiply-disposed memory manager blocks. [You can find these extensions in many places, including our ftp and other online sites - Ed stb] Run your application. You may be surpised at what you find.
Using these debugging tools, I caught a bug in an application based on the 1/18/95 release of Sprocket. This bug was fixed in the 1/25/95 Sprocket release, and does not exist in the 12/94 release. Im using this code both as an example and a suggested patch if you are using the 1/18 version of Sprocket.
TSplashWindow::~TSplashWindow()
{
if (fSplashPicture)
DisposeHandle((Handle) fSplashPicture); // Oops!
this->Close();
}
TWindow::Close(void)
{
WindowPtr newFrontWindow = nil;
if (FrontNonFloatingWindow() == fWindow)
newFrontWindow = (WindowPtr)((WindowPeek)fWindow)->nextWindow;
this->Activate(false);
DisposeWindow(fWindow);
if (newFrontWindow)
HiliteAndActivateWindow(newFrontWindow,true);
return true;
}
When I ran the executable, I dropped into my low level debugger, and looked at the stack. Return addresses on the stack from the MacsBug stack crawl sc7:
Stack Addr Frame Addr Caller
01C6ECD0 01B6D6D2 'CODE 0001 2690 Sprocket Lib'+0172
01C6ECC8 01B6D692 'CODE 0001 2690 Sprocket Lib'+0132
01C6ECAA 01C6ECA6 01B6E9F8 main+01F2
01C6EC9E 01C6EC9A 01B6EF7A TSplashWindow::~TSplashWindow()+002C
01C6EC88 01B6FA3C TWindow::Close()+0034
01C6EC80 01C6EC7C 408578C4 _DisposeWindow+0004
01C6EC6C 4085A846 _FrontWindow+0096
01C6EC54 40877EB4 _KillPicture+0004
01C6EC40 4085785C _NewWindow+010C
01C6EC34 40877EB4 _KillPicture+0004
01C6EC30 4085785C _NewWindow+010C
From this stack trace I saw that fSplashPicture got disposed again by DisposeWindow(fWindow); A review of the code showed the offending extra Dispose in ~TSplashWindow().
Why not leave these debugging resources on your system all the time? Well, it appears that not everyone uses these tools religiously and several other of my favorite (nameless) applications write to location Zero and trigger EBBE. Ive sent in bug reports to the offenders, but I am still waiting for all the update releases.
Happy Bug Trails!
- Gary W. Powell
gpowell@mv.us.adobe.com
Powering Up The RTM Instruction
For you power programmers out there, heres a little tip on taking advantage of the PowerPC architecture. Where you might have used the 68020 RTM instruction (Return from Module), you can get a lot of mileage by using the new PowerPC RTFM instruction instead. We can assure you that you will save literally hours of time.
- David Gorkan and Allan Foster
Save a Little Time
Here is a great and easy way to supercharge File Open... File Save and File Save As... operations from any application, especially if you use the Apple Menu Items to store aliases to folders: Make an alias of the Apple Menu Items folder and put it on the Desktop.
What does that get you? Well, since the Standard file Get and Put Dialogs all have a Desktop button, you can easily get to the Apple Menu Items, and hence access any aliases that are stored there. Just Click on the Desktop folder and the Apple Menu Item Alias appears as a supercharged Volume of Aliases.
But wait, theres more! You get a real bonus from using System 7.5 because, if you put the Apple Menu Items alias onto the desktop, System 7.5 maintains automated folders, so the Apple Menu Items Alias can be used to get to Recent Documents. The Recent Servers can be used to log onto to any server without leaving the application that you are running.
- Mark Tillinghast
XXCAL, Testing And Systems Division
mark_tillinghast@lamg.com
Debugging For Fun and Profit
When you have routines that return OSErrs, finish them with the following bit of code:
ReportError ( err );
return err;
}
which is defined as:
#ifdef qDEBUG
#define ReportError(err) do { if (err != noErr ) DebugStr (
\pError ); } while (false)
#else
#define ReportError(err) /* nothing */
#endif
With this macro, you can tell every time that one of your routines returns an error, and its easy to follow your error handling code.
- Marshall Clow
Aladdin Systems
Whats My Modifier?
To quickly get the status of modifier keys, use these simple lines:
typedef struct kbd_bits {
int command : 1;
int : 5;
int space : 1;
int tab : 1;
int : 4;
control : 1;
option : 1;
capslock : 1;
shift : 1;
} kbd_bits;
#define KeyModifiers (*(kbd_bits *)0x17A)
Then you can say simply:
if (KeyModifiers.control) {
Debugger(); // drop into a debugger
}
This works across all Macs, including foreign versions and keyboard maps. For maximum compatibility with a minimal speed hit, call GetOSEvent with a null event mask and examine the modifiers field of the event returned.
- Jorg Brown
The Mac Group