Skip to content

Commit

Permalink
Fix various issues with refCounts.
Browse files Browse the repository at this point in the history
  • Loading branch information
griffin committed Sep 30, 2022
1 parent 31eac27 commit b058d3e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
6 changes: 2 additions & 4 deletions generic/tclArithSeries.c
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ TclArithSeriesObjStep(
} else {
*stepObj = Tcl_NewWideIntObj(arithSeriesRepPtr->step);
}
Tcl_IncrRefCount(*stepObj);
return TCL_OK;
}

Expand Down Expand Up @@ -436,6 +437,7 @@ TclArithSeriesObjIndex(Tcl_Obj *arithSeriesPtr, Tcl_WideInt index, Tcl_Obj **ele
} else {
*elementObj = Tcl_NewWideIntObj(ArithSeriesIndexM(arithSeriesRepPtr, index));
}
Tcl_IncrRefCount(*elementObj);
return TCL_OK;
}

Expand Down Expand Up @@ -722,11 +724,8 @@ TclArithSeriesObjRange(
}

TclArithSeriesObjIndex(arithSeriesPtr, fromIdx, &startObj);
Tcl_IncrRefCount(startObj);
TclArithSeriesObjIndex(arithSeriesPtr, toIdx, &endObj);
Tcl_IncrRefCount(endObj);
TclArithSeriesObjStep(arithSeriesPtr, &stepObj);
Tcl_IncrRefCount(stepObj);

if (Tcl_IsShared(arithSeriesPtr) ||
((arithSeriesPtr->refCount > 1))) {
Expand Down Expand Up @@ -857,7 +856,6 @@ TclArithSeriesGetElements(
}
return TCL_ERROR;
}
Tcl_IncrRefCount(objv[i]);
}
}
} else {
Expand Down
7 changes: 7 additions & 0 deletions generic/tclCmdAH.c
Original file line number Diff line number Diff line change
Expand Up @@ -3027,6 +3027,13 @@ ForeachAssignments(
varValuePtr = Tcl_ObjSetVar2(interp, statePtr->varvList[i][v],
NULL, valuePtr, TCL_LEAVE_ERR_MSG);

if (isarithseries) {
/* arith values have implicit reference
** Make sure value is cleaned up when var goes away
*/
Tcl_DecrRefCount(valuePtr);
}

if (varValuePtr == NULL) {
Tcl_AppendObjToErrorInfo(interp, Tcl_ObjPrintf(
"\n (setting %s loop variable \"%s\")",
Expand Down
2 changes: 0 additions & 2 deletions generic/tclListObj.c
Original file line number Diff line number Diff line change
Expand Up @@ -2641,7 +2641,6 @@ TclLindexFlat(
}
if (i==0) {
TclArithSeriesObjIndex(listObj, index, &elemObj);
Tcl_IncrRefCount(elemObj);
} else if (index > 0) {
Tcl_DecrRefCount(elemObj);
TclNewObj(elemObj);
Expand Down Expand Up @@ -3304,7 +3303,6 @@ SetListFromAny(
if (TclArithSeriesObjIndex(objPtr, j, &elemPtrs[j]) != TCL_OK) {
return TCL_ERROR;
}
Tcl_IncrRefCount(elemPtrs[j]);/* Since list now holds ref to it. */
}

} else {
Expand Down
14 changes: 12 additions & 2 deletions tests/lseq.test
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ test lseq-3.1 {experiement} {
if {$ans eq {}} {
set ans OK
}
unset factor
unset l
set ans
} {OK}

Expand Down Expand Up @@ -376,13 +378,18 @@ test lseq-3.26 {lsort shimmer} arithSeriesShimmer {
list ${rep-before} $lexical_sort ${rep-after}
} {arithseries {0 1 10 11 12 13 14 15 2 3 4 5 6 7 8 9} arithseries}

test lseq-3.27 {lreplace shimmer} arithSeriesShimmer {
test lseq-3.27 {lreplace shimmer} -constraints arithSeriesShimmer -body {
set r [lseq 15 0]
set rep-before [lindex [tcl::unsupported::representation $r] 3]
set lexical_sort [lreplace $r 3 5 A B C]
set rep-after [lindex [tcl::unsupported::representation $r] 3]
list ${rep-before} $lexical_sort ${rep-after}
} {arithseries {15 14 13 A B C 9 8 7 6 5 4 3 2 1 0} arithseries}
} -cleanup {
unset r
unset rep-before
unset lexical_sort
unset rep-after
} -result {arithseries {15 14 13 A B C 9 8 7 6 5 4 3 2 1 0} arithseries}

test lseq-3.28 {lreverse bug in ArithSeries} {} {
set r [lseq -5 17 3]
Expand Down Expand Up @@ -499,11 +506,14 @@ test lseq-4.4 {lseq corner case} -body {
test lseq-4.5 {lindex off by one} -body {
lappend res [eval {lindex [lseq 1 4] end}]
lappend res [eval {lindex [lseq 1 4] end-1}]
} -cleanup {
unset res
} -result {4 3}


# cleanup
::tcltest::cleanupTests

return

# Local Variables:
Expand Down

0 comments on commit b058d3e

Please sign in to comment.