Michael Lawrence
2013-Apr-17 20:25 UTC
[Rd] stack imbalance in max.col for non-real matrices
It's tough to reliably reproduce, but I often get stack imbalance warnings when calling max.col() on non-real/double matrix. The code is conditionally PROTECTing but not incrementing its nprot counter for the eventual UNPROTECT. Pretty sure this would fix (but I haven't tested it): Index: array.c ==================================================================--- array.c (revision 61606) +++ array.c (working copy) @@ -1614,7 +1614,10 @@ SEXP m = CAR(args); int method = asInteger(CADR(args)); int nr = nrows(m), nc = ncols(m), nprot = 1; - if (TYPEOF(m) != REALSXP) PROTECT(m = coerceVector(m, REALSXP)); + if (TYPEOF(m) != REALSXP) { + PROTECT(m = coerceVector(m, REALSXP)); + nprot++; + } SEXP ans = allocVector(INTSXP, nr); PROTECT(ans); R_max_col(REAL(m), &nr, &nc, INTEGER(ans), &method); Thanks, Michael [[alternative HTML version deleted]]
Prof Brian Ripley
2013-Apr-19 08:16 UTC
[Rd] stack imbalance in max.col for non-real matrices
On 17/04/2013 21:25, Michael Lawrence wrote:> It's tough to reliably reproduce, but I often get stack imbalance warnings > when calling max.col() on non-real/double matrix. The code is conditionally > PROTECTing but not incrementing its nprot counter for the eventual > UNPROTECT. Pretty sure this would fix (but I haven't tested it):I can't reproduce it either, but that seems the right fix so incorporated now, thanks.> > Index: array.c > ==================================================================> --- array.c (revision 61606) > +++ array.c (working copy) > @@ -1614,7 +1614,10 @@ > SEXP m = CAR(args); > int method = asInteger(CADR(args)); > int nr = nrows(m), nc = ncols(m), nprot = 1; > - if (TYPEOF(m) != REALSXP) PROTECT(m = coerceVector(m, REALSXP)); > + if (TYPEOF(m) != REALSXP) { > + PROTECT(m = coerceVector(m, REALSXP)); > + nprot++; > + } > SEXP ans = allocVector(INTSXP, nr); > PROTECT(ans); > R_max_col(REAL(m), &nr, &nc, INTEGER(ans), &method); > > Thanks, > Michael > > [[alternative HTML version deleted]] > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595