[futurebasic] Re: [FB] app resource gets corrupted

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : August 2003 : Group Archive : Group : All Groups

From: Alain Pastor <apastor@...>
Date: Thu, 07 Aug 2003 21:14:26 +0200

MoorePrint@... wrote:
> Alain wrote:
> <<n Carbon, you can check the validity of your handle with fn 
>  IsHandleValid. In PPC you must emulate that function maybe like so:
>  
>  #If CarbonLib = 0
>  Local Mode
>  Local Fn IsHandleValid( H As Handle )
>  End Fn = ( H != _nil And Fn HGetState( H ) != -1 )
>  #Endif
>  
> 
> 
> Does this mean that checking for handle not zero isn't good enough? I do stuff like
> 
> hndl&=fn newhandleclear(2)
> long if hndle&
> 'do stuff
> end if
> 
> or if it's not a new handle it is FN GETRESOURCE or FN InstallPop... 
> Should I use the new test to see if the handle is valid in these cases.. resource pulled from external file, pulled from app file, new resource handle, popup menu handle?
> 

It depends on the function you are using. Most of the times the 
Toolbox functions return a _nil handle when they fail. This is the 
case with NewHandleClear, so checking if the handle variable is non 
zero right after calling that function is OK.
Suppose now you use DisposeHandle, or perhaps ReleaseResource with a 
resource handle, this kind of checking is now useless unless you have 
set your handle variable to zero at the same time of course.
I think this is a possible scenario where you can end up with a 
corrupted resource, that's why I have suggested to use IsHandleValid 
before storing the resources. Note that my emulation for PPC is not 
correct. I know this technique has worked for me in the past, and I 
don't know why it doesn't work any longer. There's no problem in 
Carbon as shown below:


'~'A
'                       Runtime : Rntm Appearance.Incl
'                           CPU : Carbon
'                    CALL Req'd : Off
'~'B

dim hndle&

hndle& = fn NewHandleClear(2)
long if hndle&
print "Handle valid"
xelse
print "Handle not valid"
end if

DisposeHandle(hndle&)

Print "Checking for nil handle: ";
long if hndle&
print "Handle valid"
xelse
print "Handle not valid"
end if

Print "Checking for validity: ";
long if fn IsHandleValid(hndle&)
print "Handle valid"
xelse
print "Handle not valid"
end if

do
handleevents
until 0

Alain