[comp.sys.mac.programmer] My problems with AddResource

DN5@PSUVM.BITNET (04/14/89)

I am trying to add a resource to a HyperCard stack.  I have done this
successfully before, but I can't get it to work now.  I copied the code
from my working version (which still works), but very odd things happen.

My XCMD does the following:

    aMyRecHandle := MyRecHandle (NewHandle (SizeOf (MyRec)));

    AddResource (Handle (aMyRecHandle), 'Jfmt', 129, '');
    SetResAttrs (Handle (aMyRecHandle), resPurgeable);
    UpdateResFile (CurResFile);

    ReleaseResource (Handle (aMyRecHandle));
    DisposHandle (Handle (aMyRecHandle));


There is also some error checking, and for testing purposes, this routine
returns the number of bytes in the handle created (in the result, which
equals 100).  There are also some error checking statements, put in when
the problems started.  They have never been invoked.

The problem:  After I call the XCMD, I check the stack with resEdit.  The
resource is created, and the stacks resource fork grows by either 124 or 24
bytes (the first oddity).  When I get info on the resource, ResEdit claims
that it has a length that can run in the millions of bytes.  If I delete the
resource (with clear), and call the XCMD again, it may have a different
length.

However, the XCMD has always claimed to make a constant 100 byte handle.  I
am not even trying to do anything to the handle (like put data in it yet),
just trying to get the **** thing created properly.

Anybody out there have any ideas??????


                           Jay, etc... (D. Jay Newman)

tim@hoptoad.uucp (Tim Maroney) (04/17/89)

In article <81471DN5@PSUVM> DN5@PSUVM.BITNET writes:
>I am trying to add a resource to a HyperCard stack.  I have done this
>successfully before, but I can't get it to work now.  I copied the code
>from my working version (which still works), but very odd things happen.
>
>My XCMD does the following:
>
>    ReleaseResource (Handle (aMyRecHandle));
>    DisposHandle (Handle (aMyRecHandle));

Don't call DisposHandle after ReleaseResource.  ReleaseResource has
already done it.  Disposing of a handle twice is a good way to corrupt
the heap.  You were just lucky that it worked before.  In fact, I
wouldn't even call ReleaseResource.  An unlocked, purgeable handle is
not too likely to cause memory problems.  Just add it; from that point
on, the Resource Manager can take care of it.
-- 
Tim Maroney, Consultant, Eclectic Software, sun!hoptoad!tim
"There's a real world out there, with real people.  Go out and play there for
 a while and give the Usenet sandbox a rest.  It will lower your stress
 levels and make the world a happier place for us all." -- Gene Spafford

brecher@well.UUCP (Steve Brecher) (04/17/89)

In article <81471DN5@PSUVM>, DN5@PSUVM.BITNET writes:

>     AddResource (Handle (aMyRecHandle), 'Jfmt', 129, '');
>     SetResAttrs (Handle (aMyRecHandle), resPurgeable);
>     UpdateResFile (CurResFile);

AddResource sets the resChanged resource attribute.  The SetResAttrs call
above will clear resChanged.  See IM I-122, SetResAttrs -- but ignore the
cited necessity of calling ChangedResource in any case, as SetResAttrs
sets the mapChanged resource map attribute; in your case, AddResource
sets mapChanged also.

>     ReleaseResource (Handle (aMyRecHandle));
>     DisposHandle (Handle (aMyRecHandle));

ReleaseResource is equivalent to DetachResource followed by DisposHandle.
See IM I-120, ReleaseResource.
-- 

brecher@well.UUCP (Steve Brecher)

darin@Apple.COM (Darin Adler) (04/20/89)

In article <81471DN5@PSUVM> DN5@PSUVM.BITNET writes:
> I am trying to add a resource to a HyperCard stack.  I have done this
> successfully before, but I can't get it to work now.  I copied the code
> from my working version (which still works), but very odd things happen.
> 
> My XCMD does the following:
> 
>     aMyRecHandle := MyRecHandle (NewHandle (SizeOf (MyRec)));
> 
>     AddResource (Handle (aMyRecHandle), 'Jfmt', 129, '');
>     SetResAttrs (Handle (aMyRecHandle), resPurgeable);
>     UpdateResFile (CurResFile);
> 
>     ReleaseResource (Handle (aMyRecHandle));
>     DisposHandle (Handle (aMyRecHandle));

The problem here is that you are calling SetResAttrs, which clears the
resChanged bit in your resource. Quoting from Inside Mac Vol. I-122:

	"Warning: Do not use SetResAttrs to set the resChanged attribute;
	 you must call ChangedResource instead. Be sure that the attrs
	 parameter passed to SetResAttrs does not change the current
	 setting of this attribute; to determine the current setting, first
	 call GetResAttrs."

You can use this instead:

	SetResAttrs(handle, BOR(GetResAttrs(handle), resPurgeable);
-- 
Darin Adler, System Software Scapegoat, Apple Computer
	Internet: darin@Apple.com
	UUCP: {nsc,sun}!apple!darin