Forum Navigation
You need to log in to create posts and topics.

Re: When to dispose of Hndl& and Ptr&?

Posted by: wave <wave@...>

It's been a long time since I've used the resource manager, but here goes...

>I am confused about when I should (or should not) include statements to
>dispose of handles and/or pointers. Consider the FN below that creates a
>resource file and does two things that got me to this state:
>
> CALL RELEASERESOURCE(H&)
> DEF DISPOSEH(H&)
>
>First, I read in a technical note, that we should never need to use CALL
>RELEASERESOURCE.

Could you specify which Technote this is--I read somewhere that you always
had to use CALL RELEASERESOURCE on a resource handle, but this information
may be obsolete now.

>Secondly, do we really need to dispose of handle H& since
>it is in an FN and will be essentially done away with when we exit the FN?
>(I don't know why, but, I feel I just asked a stupid question).

A very smart question actually, since it goes back to the meaning of a
handle. Remember that a handle, in a way, points to a memory block
(actually it points to a master pointer to a memory block, but you probably
already know that). A handle is also just a variable. If we want, we can
take a handle value and set it to zero like this:
h&=0
This might look like we are clearing the memory block, but all we are doing
is losing track of it. The memory block is still there, duly allocated by
the Mac OS, until the Mac OS clears it. This is why we need to call
DISPOSHANDLE.

The same is true when you create a memory block inside a function like the
one you have below. Even though the function will dispose of the h& value
on exit, you still need to explicitly call DISPOSHANDLE to have the Mac OS
clear the memory block.

>I have seen several routines that use statements like these and then other
>times they don't given similar conditions. Anyone have all this worked out
>and could summarize it for me? BTW, this routine works fine if I give it a
>filename that doesn't exist in the folder, otherwise it crashes on
>duplicating filenames - just after the "do you want to replace existing
>file?".

Three suggestions:
1. Try making your memory block larger. Right now it is 2 bytes and you
are putting multi-byte strings into it. At least make the handle large
enough to hold both strings (unless FB does this automatically with the DEF
APNDSTR command).
2. Delete the RELEASERESOURCE call. Right now you are disposing of your
memory block twice. By the way, it's always good to write the DISPOSHANDLE
call like this for safety
IF H& THEN DEF DISPOSEH(H&)
This way, if you've already disposed of the handle then you won't try to
dispose of it twice and possibly cause a system crash.
3. Try disposing of the handle after you do the CLOSERESFILE call. It's
my understanding that changed resources are written to disk when a res file
is closed--maybe this is still happening for some reason. If so, then the
system would be trying to write a nil handle to disk and this could cause a
problem.

Hope this helps.

>CLEAR LOCAL
>DIM 80 mystring$(2)
>DIM volRefNum%,resRef%,osErr%,x%
>DIM H&
>LOCAL FN createRes
> mystring$(1) = "First string in this STR# resource."
> mystring$(2) = "Second string in this STR# resource."
> filename$ = FILES$(_fSave,"Create fileŠ",,volRefNum%)
> LONG IF filename$ ""
> volRefNum% = FOLDER("",volRefNum%)
> CALL CREATERESFILE(filename$)
> resRef% = FN OPENRESFILE(filename$)
> LONG IF resRef%
> H& = FN NEWHANDLE _clear(2)
> LONG IF H&
> osErr% = FN HLOCK(H&)
> POKE WORD [H&],0
> FOR X% = 1 TO 2
> DEF APNDSTR(mystring$(X%),H&)
> NEXT X%
> CALL ADDRESOURCE(H&,_"STR#",128,"")
> CALL UPDATERESFILE(resRef%)
> osErr% = FN HUNLOCK(H&)
> CALL RELEASERESOURCE(H&) '<----
> DEF DISPOSEH(H&) '<----
> END IF
> CALL CLOSERESFILE(resRef%)
> END IF
> END IF
>END FN

P.S. I'm also sending a copy of this message to you personally since my
posts don't always appear on the list. 🙁

____________________
Use e-mail with integrity.
Mark Goodes (wave@netcom.ca)