Final Cleanup

   

Practical Programming in Tcl & Tk, Third Edition
By Brent B. Welch

Table of Contents
Chapter 46.  Writing a Tk Widget in C


graphics/tip_icon.gif

When a widget is destroyed, you need to free up any resources it has allocated. The resources associated with attributes are cleaned up by Tk_FreeOptions. The others you must take care of yourself. The ClockDestroy procedure is called as a result of the Tk_EventuallyFree call in the ClockEventProc. The Tk_EventuallyFree procedure is part of a protocol that is needed for widgets that might get deleted when in the middle of processing. Typically the Tk_Preserve and Tk_Release procedures are called at the beginning and end of the widget instance command to mark the widget as being in use. Tk_EventuallyFree will wait until Tk_Release is called before calling the cleanup procedure. The next example shows ClockDestroy:

Example 46-16 The ClockDestroy cleanup procedure.
 static void ClockDestroy(clientData)    ClientData clientData;      /* Info about entry widget. */ {    register Clock *clockPtr = (Clock *) clientData;    /*     * Free up all the stuff that requires special handling,     * then let Tk_FreeOptions handle resources associated     * with the widget attributes.     */    if (clockPtr->textGC != None) {       Tk_FreeGC(clockPtr->display, clockPtr->textGC);    }    if (clockPtr->clock != NULL) {       Tcl_Free(clockPtr->clock);    }    if (clockPtr->flags & TICKING) {       Tk_DeleteTimerHandler(clockPtr->token);    }    if (clockPtr->flags & REDRAW_PENDING) {       Tk_CancelIdleCall(ClockDisplay,          (ClientData) clockPtr);    }    /*     * This frees up colors and fonts and any allocated     * storage associated with the widget attributes.     */    Tk_FreeOptions(configSpecs, (char *) clockPtr,       clockPtr->display, 0);    Tcl_Free((char *) clockPtr); } 

The version of ClockDestroy that uses the Tcl_Obj interfaces calls Tk_FreeConfigOptions instead of Tk_FreeOptions. The ClockObjDelete command is called when the oclock command is removed from the interpreter. This has to clean up the option table used to parse options, if it has been initialized. There is no corresponding delete procedure for the string-based version of the widget. Example 46-17 shows ClockObjDelete.

Example 46-17 The ClockObjDelete command.
 void ClockObjDelete(ClientData clientData) {    Tk_OptionTable optionTable = (Tk_OptionTable) clientData;    if (optionTable != NULL) {       Tk_DeleteOptionTable(optionTable);    } } 

       
    Top
     



    Practical Programming in Tcl and Tk
    Practical Programming in Tcl and Tk (4th Edition)
    ISBN: 0130385603
    EAN: 2147483647
    Year: 1999
    Pages: 478

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net