TweetFollow Us on Twitter

Apr 96 Tips
Volume Number:12
Issue Number:4
Column Tag:Tips & Tidbits

Tips & Tidbits

By Steve Sisak

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

Avoid RemoveResource, It’ll Get You

I helped a friend with this recently:

Avoid RemoveResource (or, if you’re nstlgic for stupid names, RmveResource) if possible. If you need to replace a resource, like in a preference file, then resize the resource handle if necessary, copy the new data into it, and write it out. This tends to increase very slightly the possibility “of out of memory” failures due to heap fragmentation, but it avoids the only situation (to my knowledge) where using Resource Manager calls in a straightforward way leaves you open to file corruption. RemoveResource immediately shortens the resource file by the length of a resource map record. If the catalog is flushed, and you then crash or otherwise terminate a program without calling UpdateResFile, you’re hosed. Parts of the resource map at the end of the file are snipped off, very likely rendering the file unusable by the resource manager.

I encapsulated the resize/copy/write stuff into a routine I called ReplaceResource. If you use RemoveResource/AddResource and crash, you could lose the whole file. If you use ReplaceResource and crash, all you’ll lose is the recently added data. I like the failure mode where I get to keep most of my data.

(Historical anecdote: I learned about this behavior during System 7 development. The bug report said something like, “Changing the control panel settings and then hitting the reset switch makes the system fail to boot.” The system file was being shortened by the RemoveResource calls. I think we fixed it by calling UpdateResFile(2) immediately after removing and adding new resources, mostly because it was the quickest way to get that bug out of our faces and move on to the other two or three thousand. I recommend the ReplaceResource”approach, though, because there are times when calling UpdateResFile isn’t appropriate for performance reasons and it’s good to use one technique everywhere, instead of “sometimes you do this, sometimes you do that...”)

Greg Marriott

Fat Apps Can Purge Unwanted 'CODE's,
(But They’ll Still Be Fat)

I can’t remember who came up with this one originally, but it’s a good one:

68K development environments like to mark your first one or two 'CODE' segments as preloaded. For fat applications, though, these resources just take up valuable heap since, they never are used but don’t get purged.

Something like the following should fix the problem:

#if GENERATINGPOWERPC
 Handle h;
 h = Get1Resource( 'CODE', 0 );
 if ( h ) ReleaseResource( h );

 h = Get1Resource( 'CODE', 1 );
 if ( h ) ReleaseResource( h );
#endif

Note that it is important to use GENERATINGPOWERPC instead of GENERATINGCFM, because it is possible for GENERATINGCFM to be defined under 68K as well. And if you are building a 68K CFM app, you still end up using code segments....

Eric Shapiro

[Special thanks to Allan Foster and Marshall Clow on this one - the reward is being donated to charity. - sgs]

 

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.