[futurebasic] sheet alerts from nibs and user data

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : December 2009 : Group Archive : Group : All Groups

From: "Edwards, Waverly" <Waverly.Edwards@...>
Date: Tue, 29 Dec 2009 17:09:10 -0600
I'm using nib's windows as sheets.  I have the standard window handler checked in the nib window attributes.

1. Is the standard event handler supposed to hide the sheet?
2. Anyone know if you are suppose to dispose of the user data that you create or is it done when you remove the event handler.

When I "free" my data, upon the third time I've shown and removed the sheet, the program crashes.
I dont want to have a data leak and I dont know how to determine whether the data was already destroyed.

I remove the event handler because you can only have one handler of the same type per window.  The window may have
another sheet which *may* use the same handler.  To avoid failure, I remove the handler.

These are modified versions of RP's  "Sheet Alerts (method 1)" from 2005, if it matters.


Thanks,


W.



begin record SheetInfo
dim as SInt32    sheetNumRef
dim as WindowRef sheetW
dim as WindowRef parentW
end record


local fn DismissSheet( nextHandler as EventHandlerCallRef, theEvent as EventRef, userData as pointer to SheetInfo  ) 
'~'1
dim as SInt32       sheetNumRef
dim as OSStatus     result, ignore, err
dim as HICommand    command
dim as WindowRef    sheetW, parentW


result = _eventNotHandledErr
ignore = fn GetEventParameter( theEvent, _kEventParamDirectObject, _typeHICommand, #0, sizeof( HICommand ), #0, @command )
long if ( command.attributes == _kHICommandFromControl ) // reject possible extraneous commands
select command.commandID 
// we are interested in 3 commands 
 case _kHICommandOK, _kHICommandCancel, _kHICommandOther
result = fn CallNextEventHandler( nextHandler, theEvent ) // let the standard handler close the sheet

sheetW      = userData.sheetW
parentW     = userData.parentW
sheetNumRef = userData.sheetNumRef


select command.commandID 
case _kHICommandOK

case _kHICommandCancel

case _kHICommandOther

end select


err = fn HideSheetWindow( sheetW )
DisposeWindow( sheetW )
err = fn RemoveEventHandler( fn GetWindowEventTarget( parentW ) )

#if 0
//free( userData ) // causes crash, but why?
#endif
end select
end if

end fn = result


local fn InstallSheetHandler( parentW as WindowRef, sheetW as WindowRef, sheetNumRef as long ) as OSStatus
'~'1
dim as EventTypeSpec     eventType
dim as ptr to SheetInfo  userData

eventType.eventClass = _kEventClassCommand
eventType.eventKind  = _kEventProcessCommand

userData             = fn malloc( sizeof( SheetInfo ) )
userData.sheetW      = sheetW
userData.parentW     = parentW
userData.sheetNumRef = sheetNumRef

end fn = fn InstallEventHandler( fn GetWindowEventTarget( parentW ), @fn DismissSheet, 1, @eventType, #userData, #0 )