[comp.sys.mac.programmer] THINK C Help with Resources

gilbert@grad1.cis.upenn.edu (Michael Gilbert) (10/04/90)

I have a simple problem (well, not to me), which I encountered
recently during my slow, but unsteady progress in learning Mac C
programming.

My problem is in creating a "save file."  I have an object pointed-to
by a handle, such as:

	ThingHandle myThingHandle;

I want to store this thing as a resource in a file.  So, I first call
CreateResFile (myFileName); (with appropriate error-checks), and
then UseResFile (myFileName) to make use of the file.  Finally, I use

	AddResource ((Handle) myThingHandle ...)

	to add the resource.  Now, using ResEdit, I can see that the
resource was indeed added correctly to this file.  However, after
saving this data to the file and closing it, the data in my object is 
corrupted.  Is there some step I'm missing, possibly involving
HandToHand, that would keep the data stable?

Any help and code snippets would be appreciated, by posting or e-mail.

===============================================================================
| CONSIDER IT DISCLAIMED               | #include <disclaim.h>                |
|-----------------------------------------------------------------------------|
|  _ _ _                               | Better the pride that resides,       |
| ' ) ) )   /                          | In a citizen of the world,           |
|  / / / o /_  _                       | Than the pride that divides,         |
| / ' (_<_/ <_</_                      | When a colourful rag is unfurled     |
| gilbert@grad1.cis.upenn.edu          |      --- Neil Peart                  |
|-----------------------------------------------------------------------------|
| CZECHOSLOVAKIA CHIEFS --- IGBA Champs 1984-1985 1986-Tourney 1987 1988-1989 |
===============================================================================

nick@cs.edinburgh.ac.uk (Nick Rothwell) (10/05/90)

In article <30543@netnews.upenn.edu>, gilbert@grad1.cis.upenn.edu (Michael Gilbert) writes:
> I want to store this thing as a resource in a file.  So, I first call
> CreateResFile (myFileName); (with appropriate error-checks), and
> then UseResFile (myFileName) to make use of the file.  Finally, I use
> 
> 	AddResource ((Handle) myThingHandle ...)
> 
> 	to add the resource.  Now, using ResEdit, I can see that the
> resource was indeed added correctly to this file.  However, after
> saving this data to the file and closing it, the data in my object is 
> corrupted.

I don't have my copy of Inside Mac to hand, and I haven't actually
written applications which create resources, so this is a rough guess,
but it's possible the handle is getting purged and/or moved as soon
as you flush it to the file. In all my Mac programming, I always wrap
calls to H{Un}Lock and H{No}Purge around anything I'm not certain about.

Just an idea...


Nick Rothwell,	Laboratory for Foundations of Computer Science, Edinburgh.
		nick@lfcs.ed.ac.uk    <Atlantic Ocean>!mcsun!ukc!lfcs!nick
~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~
 "Now remember - and this is most important - you must think in Russian."

phils@chaos.cs.brandeis.edu (Phil Shapiro) (10/08/90)

In article <522@skye.cs.ed.ac.uk> nick@cs.edinburgh.ac.uk (Nick Rothwell) writes:
>   In article <30543@netnews.upenn.edu>, gilbert@grad1.cis.upenn.edu (Michael Gilbert) writes:
>   > I want to store this thing as a resource in a file.  So, I first call
>   > CreateResFile (myFileName); (with appropriate error-checks), and
>   > then UseResFile (myFileName) to make use of the file.  Finally, I use
>   > 
>   > 	AddResource ((Handle) myThingHandle ...)
>   > 
>   > 	to add the resource.  Now, using ResEdit, I can see that the
>   > resource was indeed added correctly to this file.  However, after
>   > saving this data to the file and closing it, the data in my object is 
>   > corrupted.
>
>   I don't have my copy of Inside Mac to hand, and I haven't actually
>   written applications which create resources, so this is a rough guess,
>   but it's possible the handle is getting purged and/or moved as soon
>   as you flush it to the file. In all my Mac programming, I always wrap
>   calls to H{Un}Lock and H{No}Purge around anything I'm not certain about.

Once you call AddResource, the handle is a resource handle.  It
belongs to the resource file that you added it to.  Once you close the
resource file, the handle is no longer valid (all resources in the
closed file are deallocated).  The solution is to either (1) call
HandToHand to copy it, or (2) call DetachResource after adding the
handle to the file, but before you close it.  (Look at IM I-124).

	-phil shapiro, symantec tech support
--
Phil Shapiro
phils@chaos.cs.brandeis.edu