Index: generic/tclBinary.c
===================================================================
RCS file: /cvsroot/tcl/tcl/generic/tclBinary.c,v
retrieving revision 1.26
diff -u -r1.26 tclBinary.c
--- generic/tclBinary.c	27 Sep 2005 15:20:35 -0000	1.26
+++ generic/tclBinary.c	4 Nov 2005 22:26:26 -0000
@@ -1085,7 +1085,6 @@
 		arg++;
 		if (resultPtr == NULL) {
 		    DeleteScanNumberCache(numberCachePtr);
-		    Tcl_DecrRefCount(valuePtr);	/* unneeded */
 		    return TCL_ERROR;
 		}
 		offset += count;
@@ -1140,7 +1139,6 @@
 		arg++;
 		if (resultPtr == NULL) {
 		    DeleteScanNumberCache(numberCachePtr);
-		    Tcl_DecrRefCount(valuePtr);	/* unneeded */
 		    return TCL_ERROR;
 		}
 		offset += (count + 7 ) / 8;
@@ -1197,7 +1195,6 @@
 		arg++;
 		if (resultPtr == NULL) {
 		    DeleteScanNumberCache(numberCachePtr);
-		    Tcl_DecrRefCount(valuePtr);	/* unneeded */
 		    return TCL_ERROR;
 		}
 		offset += (count + 1) / 2;
@@ -1267,7 +1264,6 @@
 		arg++;
 		if (resultPtr == NULL) {
 		    DeleteScanNumberCache(numberCachePtr);
-		    Tcl_DecrRefCount(valuePtr);	/* unneeded */
 		    return TCL_ERROR;
 		}
 		break;
Index: generic/tclCmdAH.c
===================================================================
RCS file: /cvsroot/tcl/tcl/generic/tclCmdAH.c,v
retrieving revision 1.69
diff -u -r1.69 tclCmdAH.c
--- generic/tclCmdAH.c	8 Oct 2005 14:42:44 -0000	1.69
+++ generic/tclCmdAH.c	4 Nov 2005 22:26:27 -0000
@@ -267,7 +267,6 @@
 	Tcl_Obj *options = Tcl_GetReturnOptions(interp, result);
 	if (NULL == Tcl_ObjSetVar2(interp, optionVarNamePtr, NULL,
 		options, 0)) {
-	    Tcl_DecrRefCount(options);
 	    Tcl_ResetResult(interp);
 	    Tcl_AppendResult(interp,
 		    "couldn't save return options in variable", NULL);
@@ -1485,7 +1484,6 @@
     value = (object); \
     if (Tcl_ObjSetVar2(interp,varName,field,value,TCL_LEAVE_ERR_MSG)==NULL) { \
 	Tcl_DecrRefCount(field); \
-	Tcl_DecrRefCount(value); \
 	return TCL_ERROR; \
     }
 
@@ -1805,20 +1803,15 @@
 	    for (v=0 ; v<varcList[i] ; v++) {
 		int k = index[i]++;
 		Tcl_Obj *valuePtr, *varValuePtr;
-		int isEmptyObj = 0;
 
 		if (k < argcList[i]) {
 		    valuePtr = argvList[i][k];
 		} else {
 		    valuePtr = Tcl_NewObj(); /* empty string */
-		    isEmptyObj = 1;
 		}
 		varValuePtr = Tcl_ObjSetVar2(interp, varvList[i][v],
 			NULL, valuePtr, 0);
 		if (varValuePtr == NULL) {
-		    if (isEmptyObj) {
-			Tcl_DecrRefCount(valuePtr);
-		    }
 		    Tcl_ResetResult(interp);
 		    Tcl_AppendResult(interp, "couldn't set loop variable: \"",
 			    TclGetString(varvList[i][v]), "\"", (char *) NULL);
Index: generic/tclCmdIL.c
===================================================================
RCS file: /cvsroot/tcl/tcl/generic/tclCmdIL.c,v
retrieving revision 1.83
diff -u -r1.83 tclCmdIL.c
--- generic/tclCmdIL.c	19 Oct 2005 18:39:58 -0000	1.83
+++ generic/tclCmdIL.c	4 Nov 2005 22:26:29 -0000
@@ -1012,7 +1012,6 @@
 		valueObjPtr = Tcl_ObjSetVar2(interp, objv[4], NULL,
 			nullObjPtr, 0);
 		if (valueObjPtr == NULL) {
-		    Tcl_DecrRefCount(nullObjPtr); /* free unneeded obj */
 		    goto defStoreError;
 		}
 		Tcl_SetObjResult(interp, Tcl_NewIntObj(0));
@@ -2260,7 +2259,8 @@
     Tcl_Obj **listObjv;		/* The contents of the list. */
     int listObjc;		/* The length of the list. */
     int i;
-
+    Tcl_Obj *resPtr;
+    
     if (objc < 3) {
 	Tcl_WrongNumArgs(interp, 1, objv, "list varname ?varname ...?");
 	return TCL_ERROR;
@@ -2294,15 +2294,15 @@
 	 */
 
 	Tcl_IncrRefCount(valueObj);
-	if (Tcl_ObjSetVar2(interp, objv[i+2], NULL, valueObj,
-		TCL_LEAVE_ERR_MSG) == NULL) {
-	    Tcl_DecrRefCount(valueObj);
+	resPtr = Tcl_ObjSetVar2(interp, objv[i+2], NULL, valueObj,
+		TCL_LEAVE_ERR_MSG);
+	TclDecrRefCount(valueObj);
+	if (resPtr == NULL) {
 	    if (emptyObj != NULL) {
 		Tcl_DecrRefCount(emptyObj);
 	    }
 	    return TCL_ERROR;
 	}
-	Tcl_DecrRefCount(valueObj);
     }
     if (emptyObj != NULL) {
 	Tcl_DecrRefCount(emptyObj);
Index: generic/tclCmdMZ.c
===================================================================
RCS file: /cvsroot/tcl/tcl/generic/tclCmdMZ.c,v
retrieving revision 1.132
diff -u -r1.132 tclCmdMZ.c
--- generic/tclCmdMZ.c	8 Oct 2005 14:42:44 -0000	1.132
+++ generic/tclCmdMZ.c	4 Nov 2005 22:26:31 -0000
@@ -367,7 +367,6 @@
 		Tcl_Obj *valuePtr;
 		valuePtr = Tcl_ObjSetVar2(interp, objv[i], NULL, newPtr, 0);
 		if (valuePtr == NULL) {
-		    Tcl_DecrRefCount(newPtr);
 		    Tcl_AppendResult(interp, "couldn't set variable \"",
 			    TclGetString(objv[i]), "\"", (char *) NULL);
 		    return TCL_ERROR;
@@ -2730,7 +2729,6 @@
 		TclNewObj(emptyObj);
 		if (Tcl_ObjSetVar2(interp, indexVarObj, NULL, emptyObj,
 			TCL_LEAVE_ERR_MSG) == NULL) {
-		    Tcl_DecrRefCount(emptyObj);
 		    return TCL_ERROR;
 		}
 	    }
@@ -2740,9 +2738,6 @@
 		}
 		if (Tcl_ObjSetVar2(interp, matchVarObj, NULL, emptyObj,
 			TCL_LEAVE_ERR_MSG) == NULL) {
-		    if (indexVarObj == NULL) {
-			Tcl_DecrRefCount(emptyObj);
-		    }
 		    return TCL_ERROR;
 		}
 	    }
@@ -2829,8 +2824,6 @@
 	if (indexVarObj != NULL) {
 	    if (Tcl_ObjSetVar2(interp, indexVarObj, NULL, indicesObj,
 		    TCL_LEAVE_ERR_MSG) == NULL) {
-		Tcl_DecrRefCount(indicesObj);
-
 		/*
 		 * Careful! Check to see if we have allocated the list of
 		 * matched strings; if so (but there was an error assigning
@@ -2848,8 +2841,6 @@
 	if (matchVarObj != NULL) {
 	    if (Tcl_ObjSetVar2(interp, matchVarObj, NULL, matchesObj,
 		    TCL_LEAVE_ERR_MSG) == NULL) {
-		Tcl_DecrRefCount(matchesObj);
-
 		/*
 		 * Unlike above, if indicesObj is non-NULL at this point, it
 		 * will have been written to a variable already and will hence
Index: generic/tclDictObj.c
===================================================================
RCS file: /cvsroot/tcl/tcl/generic/tclDictObj.c,v
retrieving revision 1.38
diff -u -r1.38 tclDictObj.c
--- generic/tclDictObj.c	1 Nov 2005 15:30:52 -0000	1.38
+++ generic/tclDictObj.c	4 Nov 2005 22:26:32 -0000
@@ -1963,18 +1963,17 @@
 	    Tcl_DecrRefCount(incrPtr);
 	}
     }
-    Tcl_IncrRefCount(dictPtr);
     if (code == TCL_OK) {
 	Tcl_InvalidateStringRep(dictPtr);
 	valuePtr = Tcl_ObjSetVar2(interp, objv[2], NULL,
 		dictPtr, TCL_LEAVE_ERR_MSG);
 	if (valuePtr == NULL) {
 	    code = TCL_ERROR;
+	} else {
+	    Tcl_SetObjResult(interp, valuePtr);
 	}
-    }
-    Tcl_DecrRefCount(dictPtr);
-    if (code == TCL_OK) {
-	Tcl_SetObjResult(interp, valuePtr);
+    } else if (dictPtr->refCount == 0) {
+	Tcl_DecrRefCount(dictPtr);
     }
     return code;
 }
@@ -2056,10 +2055,8 @@
 	Tcl_InvalidateStringRep(dictPtr);
     }
 
-    Tcl_IncrRefCount(dictPtr);
     resultPtr = Tcl_ObjSetVar2(interp, objv[2], NULL, dictPtr,
 	    TCL_LEAVE_ERR_MSG);
-    TclDecrRefCount(dictPtr);
     if (resultPtr == NULL) {
 	return TCL_ERROR;
     }
@@ -2129,10 +2126,8 @@
 
     Tcl_DictObjPut(interp, dictPtr, objv[3], valuePtr);
 
-    Tcl_IncrRefCount(dictPtr);
     resultPtr = Tcl_ObjSetVar2(interp, objv[2], NULL, dictPtr,
 	    TCL_LEAVE_ERR_MSG);
-    TclDecrRefCount(dictPtr);
     if (resultPtr == NULL) {
 	return TCL_ERROR;
     }
@@ -2309,10 +2304,8 @@
 	return TCL_ERROR;
     }
 
-    Tcl_IncrRefCount(dictPtr);
     resultPtr = Tcl_ObjSetVar2(interp, objv[2], NULL, dictPtr,
 	    TCL_LEAVE_ERR_MSG);
-    TclDecrRefCount(dictPtr);
     if (resultPtr == NULL) {
 	return TCL_ERROR;
     }
@@ -2369,10 +2362,8 @@
 	return TCL_ERROR;
     }
 
-    Tcl_IncrRefCount(dictPtr);
     resultPtr = Tcl_ObjSetVar2(interp, objv[2], NULL, dictPtr,
 	    TCL_LEAVE_ERR_MSG);
-    TclDecrRefCount(dictPtr);
     if (resultPtr == NULL) {
 	return TCL_ERROR;
     }
@@ -2653,7 +2644,7 @@
     Tcl_Obj *CONST *objv)
 {
     Tcl_Obj *dictPtr, *objPtr;
-    int i, result, dummy, allocdict = 0;
+    int i, result, dummy;
     Tcl_InterpState state;
 
     if (objc < 6 || objc & 1) {
@@ -2716,7 +2707,6 @@
 
     if (Tcl_IsShared(dictPtr)) {
 	dictPtr = Tcl_DuplicateObj(dictPtr);
-	allocdict = 1;
     }
 
     /*
@@ -2741,9 +2731,6 @@
     if (Tcl_ObjSetVar2(interp, objv[2], NULL, dictPtr,
 	    TCL_LEAVE_ERR_MSG) == NULL) {
 	Tcl_DiscardInterpState(state);
-	if (allocdict) {
-	    TclDecrRefCount(dictPtr);
-	}
 	return TCL_ERROR;
     }
 
@@ -2921,9 +2908,6 @@
 
     if (Tcl_ObjSetVar2(interp, objv[2], NULL, dictPtr,
 	    TCL_LEAVE_ERR_MSG) == NULL) {
-	if (allocdict) {
-	    TclDecrRefCount(dictPtr);
-	}
 	Tcl_DiscardInterpState(state);
 	return TCL_ERROR;
     }
Index: generic/tclExecute.c
===================================================================
RCS file: /cvsroot/tcl/tcl/generic/tclExecute.c,v
retrieving revision 1.219
diff -u -r1.219 tclExecute.c
--- generic/tclExecute.c	2 Nov 2005 11:55:47 -0000	1.219
+++ generic/tclExecute.c	4 Nov 2005 22:26:35 -0000
@@ -5503,10 +5503,7 @@
 
 		valIndex = (iterNum * numVars);
 		for (j = 0;  j < numVars;  j++) {
-		    int setEmptyStr = 0;
-
 		    if (valIndex >= listLen) {
-			setEmptyStr = 1;
 			TclNewObj(valuePtr);
 		    } else {
 			valuePtr = elements[valIndex];
@@ -5538,9 +5535,6 @@
 			if (value2Ptr == NULL) {
 			    TRACE_WITH_OBJ(("%u => ERROR init. index temp %d: ",
 				    opnd, varIndex), Tcl_GetObjResult(interp));
-			    if (setEmptyStr) {
-				TclDecrRefCount(valuePtr);
-			    }
 			    result = TCL_ERROR;
 			    goto checkForCatch;
 			}
Index: generic/tclIOCmd.c
===================================================================
RCS file: /cvsroot/tcl/tcl/generic/tclIOCmd.c,v
retrieving revision 1.32
diff -u -r1.32 tclIOCmd.c
--- generic/tclIOCmd.c	1 Nov 2005 15:30:52 -0000	1.32
+++ generic/tclIOCmd.c	4 Nov 2005 22:26:36 -0000
@@ -289,7 +289,6 @@
     if (objc == 3) {
 	if (Tcl_ObjSetVar2(interp, objv[2], NULL, linePtr,
 		TCL_LEAVE_ERR_MSG) == NULL) {
-	    Tcl_DecrRefCount(linePtr);
 	    return TCL_ERROR;
 	}
 	Tcl_SetObjResult(interp, Tcl_NewIntObj(lineLen));
Index: generic/tclLink.c
===================================================================
RCS file: /cvsroot/tcl/tcl/generic/tclLink.c,v
retrieving revision 1.13
diff -u -r1.13 tclLink.c
--- generic/tclLink.c	8 Oct 2005 14:42:45 -0000	1.13
+++ generic/tclLink.c	4 Nov 2005 22:26:36 -0000
@@ -118,7 +118,6 @@
     if (Tcl_ObjSetVar2(interp, linkPtr->varName, NULL, objPtr,
 	    TCL_GLOBAL_ONLY|TCL_LEAVE_ERR_MSG) == NULL) {
 	Tcl_DecrRefCount(linkPtr->varName);
-	Tcl_DecrRefCount(objPtr);
 	ckfree((char *) linkPtr);
 	return TCL_ERROR;
     }
Index: generic/tclTest.c
===================================================================
RCS file: /cvsroot/tcl/tcl/generic/tclTest.c,v
retrieving revision 1.98
diff -u -r1.98 tclTest.c
--- generic/tclTest.c	2 Nov 2005 15:59:48 -0000	1.98
+++ generic/tclTest.c	4 Nov 2005 22:26:39 -0000
@@ -3843,7 +3843,6 @@
 	}
 	valuePtr = Tcl_ObjSetVar2(interp, varPtr, NULL, newPtr, 0);
 	if (valuePtr == NULL) {
-	    Tcl_DecrRefCount(newPtr);
 	    Tcl_AppendResult(interp, "couldn't set variable \"",
 		    Tcl_GetString(varPtr), "\"", NULL);
 	    return TCL_ERROR;
Index: generic/tclVar.c
===================================================================
RCS file: /cvsroot/tcl/tcl/generic/tclVar.c,v
retrieving revision 1.114
diff -u -r1.114 tclVar.c
--- generic/tclVar.c	4 Nov 2005 02:13:41 -0000	1.114
+++ generic/tclVar.c	4 Nov 2005 22:26:41 -0000
@@ -1390,10 +1390,7 @@
      */
 
     valuePtr = Tcl_NewStringObj(newValue, -1);
-    Tcl_IncrRefCount(valuePtr);
-
     varValuePtr = Tcl_SetVar2Ex(interp, part1, part2, valuePtr, flags);
-    TclDecrRefCount(valuePtr); /* done with the object */
 
     if (varValuePtr == NULL) {
 	return NULL;
@@ -1458,6 +1455,9 @@
     varPtr = TclLookupVar(interp, part1, part2, flags, "set",
 	    /*createPart1*/ 1, /*createPart2*/ 1, &arrayPtr);
     if (varPtr == NULL) {
+	if (newValuePtr->refCount == 0) {
+	    Tcl_DecrRefCount(newValuePtr);
+	}
 	return NULL;
     }
 
@@ -1514,6 +1514,9 @@
     varPtr = TclObjLookupVar(interp, part1Ptr, part2, flags, "set",
 	    /*createPart1*/ 1, /*createPart2*/ 1, &arrayPtr);
     if (varPtr == NULL) {
+	if (newValuePtr->refCount == 0) {
+	    Tcl_DecrRefCount(newValuePtr);
+	}
 	return NULL;
     }
 
@@ -1583,7 +1586,7 @@
 		TclVarErrMsg(interp, part1, part2, "set", danglingVar);
 	    }
 	}
-	return NULL;
+	goto earlyError;
     }
 
     /*
@@ -1594,7 +1597,7 @@
 	if (flags & TCL_LEAVE_ERR_MSG) {
 	    TclVarErrMsg(interp, part1, part2, "set", isArray);
 	}
-	return NULL;
+	goto earlyError;
     }
 
     /*
@@ -1606,7 +1609,7 @@
 	    || ((arrayPtr != NULL) && (arrayPtr->tracePtr != NULL)))) {
 	if (TCL_ERROR == TclCallVarTraces(iPtr, arrayPtr, varPtr, part1, part2,
 		TCL_TRACE_READS, (flags & TCL_LEAVE_ERR_MSG))) {
-	    return NULL;
+	    goto earlyError;
 	}
     }
 
@@ -1641,7 +1644,7 @@
 	    result = Tcl_ListObjAppendElement(interp, oldValuePtr,
 		    newValuePtr);
 	    if (result != TCL_OK) {
-		return NULL;
+		goto earlyError;
 	    }
 	} else {				/* append string */
 	    /*
@@ -1719,6 +1722,12 @@
 	TclCleanupVar(varPtr, arrayPtr);
     }
     return resultPtr;
+
+  earlyError:
+    if (newValuePtr->refCount == 0) {
+	Tcl_DecrRefCount(newValuePtr);
+    }
+    goto cleanup;    
 }
 
 /*
@@ -1827,7 +1836,7 @@
 				 * TCL_LEAVE_ERR_MSG. */
 {
     register Tcl_Obj *varValuePtr, *newValuePtr = NULL;
-    int code;
+    int duplicated, code;
 
     varValuePtr = TclPtrGetVar(interp, varPtr, arrayPtr, part1, part2, flags);
     if (varValuePtr == NULL) {
@@ -1836,15 +1845,18 @@
 	return NULL;
     }
     if (Tcl_IsShared(varValuePtr)) {
+	duplicated = 1;
 	varValuePtr = Tcl_DuplicateObj(varValuePtr);
+    } else {
+	duplicated = 0;
     }
     code = TclIncrObj(interp, varValuePtr, incrPtr);
-    Tcl_IncrRefCount(varValuePtr);
     if (code == TCL_OK) {
 	newValuePtr = TclPtrSetVar(interp, varPtr, arrayPtr, part1, part2,
 		varValuePtr, flags);
+    } else if (duplicated) {
+	Tcl_DecrRefCount(varValuePtr);
     }
-    Tcl_DecrRefCount(varValuePtr);
     return newValuePtr;
 }
 
@@ -2331,7 +2343,7 @@
     Tcl_Obj *CONST objv[])	/* Argument objects. */
 {
     Tcl_Obj *varValuePtr, *newValuePtr;
-    int numElems, createdNewObj, createVar;
+    int numElems, createdNewObj;
     Var *varPtr, *arrayPtr;
     char *part1;
     int result;
@@ -2352,7 +2364,6 @@
 	    newValuePtr = Tcl_ObjSetVar2(interp, objv[1], NULL, varValuePtr,
 		    TCL_LEAVE_ERR_MSG);
 	    if (newValuePtr == NULL) {
-		TclDecrRefCount(varValuePtr); /* free unneeded object */
 		return TCL_ERROR;
 	    }
 	}
@@ -2368,7 +2379,6 @@
 	 */
 
 	createdNewObj = 0;
-	createVar = 1;
 
 	/*
 	 * Use the TCL_TRACE_READS flag to ensure that if we have an array
@@ -2403,7 +2413,6 @@
 	     * create it with Tcl_ObjSetVar2 below.
 	     */
 
-	    createVar = (TclIsVarUndefined(varPtr));
 	    TclNewObj(varValuePtr);
 	    createdNewObj = 1;
 	} else if (Tcl_IsShared(varValuePtr)) {
@@ -2432,9 +2441,6 @@
 	newValuePtr = TclPtrSetVar(interp, varPtr, arrayPtr, part1, NULL,
 		varValuePtr, TCL_LEAVE_ERR_MSG);
 	if (newValuePtr == NULL) {
-	    if (createdNewObj && !createVar) {
-		TclDecrRefCount(varValuePtr); /* free unneeded obj */
-	    }
 	    return TCL_ERROR;
 	}
     }
