Re: Values being overwritten in Tcl_HashTable

Liste des GroupesRevenir à cl tcl 
Sujet : Re: Values being overwritten in Tcl_HashTable
De : francois.vogel.fv.NOSPAM (at) *nospam* gmail.com (Francois Vogel)
Groupes : comp.lang.tcl
Date : 11. Nov 2024, 23:23:20
Autres entêtes
Organisation : Guest of ProXad - France
Message-ID : <673283d9$0$434$426a74cc@news.free.fr>
References : 1
User-Agent : Mozilla Thunderbird
Le 11/11/2024 à 04:37, Kevin Walzer a écrit :
int
Tk_SetAccessibleRole(
              TCL_UNUSED(void *),
              Tcl_Interp *ip,        /* Current interpreter. */
              int objc,            /* Number of arguments. */
              Tcl_Obj *const objv[])    /* Argument objects. */
{
   if (objc < 3) {
     Tcl_WrongNumArgs(ip, 1, objv, "window? role?");
     return TCL_ERROR;
   }
    Tk_Window win;
   char *role;
   Tcl_HashEntry *hPtr, *hPtr2;
    Tcl_HashTable *AccessibleAttributes;
   AccessibleAttributes = (Tcl_HashTable *)ckalloc(sizeof(Tcl_HashTable));
   Tcl_InitHashTable(AccessibleAttributes,TCL_STRING_KEYS);
    int isNew;
   win = Tk_NameToWindow(ip, Tcl_GetString(objv[1]), Tk_MainWindow(ip));
   if (win == NULL) {
     return TCL_ERROR;
   }
    /* Set accessible role for window.  */
   hPtr=Tcl_CreateHashEntry(TkAccessibilityObject, win, &isNew);
    Tcl_SetHashValue(hPtr, AccessibleAttributes);
    hPtr2 =  Tcl_CreateHashEntry(AccessibleAttributes, role, &isNew);
   if (!isNew) {
     Tcl_DecrRefCount(Tcl_GetHashValue(hPtr2));
   }
   Tcl_IncrRefCount(objv[2]);
   Tcl_SetHashValue(hPtr2, objv[2]);
    Tcl_SetObjResult(ip, Tcl_GetHashValue(hPtr2));
   return TCL_OK;
}
Without diving deep into your code, here a handful of quick observations that may or may not be useful to you:
1. The canonical pattern for these kind of things is:
     int isNew;
     hPtr = Tcl_CreateHashEntry(tablePtr, key, &isNew);
     if (isNew) {
myValue = ...; // get or create myValue from somewhere
Tcl_SetHashValue(hPtr, myValue);
     } else {
myValue = (myValue *)Tcl_GetHashValue(hPtr);
     }
Perhaps you should stick at it.
2. You are using the isNew flag twice without checking it the first time.
3. You are calling Tcl_CreateHashEntry twice, yes, but not with the same hash table. Is this what is intended?
Regards,
Francois

Date Sujet#  Auteur
11 Nov 24 * Values being overwritten in Tcl_HashTable2Kevin Walzer
11 Nov 24 `- Re: Values being overwritten in Tcl_HashTable1Francois Vogel

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal