Skip to content

Commit 6b9aa6c

Browse files
try to PROTECT() before duplicate()
1 parent b30437b commit 6b9aa6c

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

src/mergelist.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,23 @@ SEXP cbindlist(SEXP x, SEXP copyArg) {
6565
}
6666
if (recycle)
6767
error("Rows recycling for objects of different nrow is not yet implemented"); // dont we have a routines for that already somewhere?
68-
SEXP ans = PROTECT(allocVector(VECSXP, nans));
69-
SEXP index = PROTECT(allocVector(INTSXP, 0));
68+
int protecti=0;
69+
SEXP ans = PROTECT(allocVector(VECSXP, nans)); protecti++;
70+
SEXP index = PROTECT(allocVector(INTSXP, 0)); protecti++;
7071
SEXP key = R_NilValue;
7172
setAttrib(ans, sym_index, index);
72-
SEXP names = PROTECT(allocVector(STRSXP, nans));
73+
SEXP names = PROTECT(allocVector(STRSXP, nans)); protecti++;
7374
for (int i=0, ians=0; i<nx; ++i) {
7475
SEXP thisx = VECTOR_ELT(x, i);
7576
SEXP thisnames = getAttrib(thisx, R_NamesSymbol);
7677
for (int j=0; j<nnx[i]; ++j, ++ians) {
77-
SEXP thisxcol = VECTOR_ELT(thisx, j);
78-
SET_VECTOR_ELT(ans, ians, copy ? duplicate(thisxcol) : thisxcol);
78+
SEXP thisxcol;
79+
if (copy) {
80+
thisxcol = PROTECT(duplicate(VECTOR_ELT(thisx, j))); protecti++;
81+
} else {
82+
thisxcol = VECTOR_ELT(thisx, j);
83+
}
84+
SET_VECTOR_ELT(ans, ians, thisxcol);
7985
SET_STRING_ELT(names, ians, STRING_ELT(thisnames, j));
8086
}
8187
mergeIndexAttrib(index, getAttrib(thisx, sym_index));
@@ -86,6 +92,6 @@ SEXP cbindlist(SEXP x, SEXP copyArg) {
8692
setAttrib(ans, sym_sorted, key);
8793
if (verbose)
8894
Rprintf("cbindlist: took %.3fs\n", omp_get_wtime()-tic);
89-
UNPROTECT(3);
95+
UNPROTECT(protecti);
9096
return ans;
9197
}

0 commit comments

Comments
 (0)