From hb at maths.lth.se  Fri Aug 26 17:55:38 2005
From: hb at maths.lth.se (Henrik Bengtsson)
Date: Fri, 26 Aug 2005 17:55:38 +0200
Subject: [Rd] Memory leakage/violation?
Message-ID: <430F3B7A.5020807@maths.lth.se>

Hi,

I've spotted a possible memory leakage/violation in the latest R v2.1.1 
patched and R v2.2.0dev on Windows XP Pro SP2 Eng.

I first caught it deep down in a nested svd algorithm when subtracting a 
double 'c' from a integer vector 'a' where both had finite values but 
when assigning 'a <- a - c' would report NaNs whereas (a-c) alone would 
not.  Different runs with the identical data would introduce NaNs at 
random positions, but not all the time.

Troubleshooting is after a couple of hours still at v0.5, but here is a 
script that generates the strange behavior on the above R setups.  I let 
the script speak for itself.  Note that both the script 'strange.R' and 
the data 'strange.RData' is online too, see code below.

People on other systems (but also on Windows), could you please try it 
and see if you can reproduce what I get.

Cheers

Henrik


# The following was tested on: Windows XP Pro SP2 Eng with
#   i) R Version 2.1.1 Patched (2005-08-25)
#  ii) R 2.2.0 Under development (unstable) (2005-08-25 r35394M)

# Start 'R --vanilla' and source() this script, i.e.
#  source("http://www.maths.lth.se/help/R/strange.R")
# If you do not get any errors, retry a few times.

foo <- function(x) {
   print(list(
     name=as.character(substitute(x)),
     storage.mode=storage.mode(x),
     na=any(is.na(x)),
     nan=any(is.nan(x)),
     inf=any(is.infinite(x)),
     ok=all(is.finite(a))
   ))
   print(length(x))
   print(summary(x))
}

# Load data from a complicated "non-reproducible" algorithm.
# The below errors occur also when data is not
# saved and then reloaded from file.  Data was generated in
# R v2.1.1 patched (see above).
if (file.exists("strange.RData")) {
   load("strange.RData")
} else {
   load(url("http://www.maths.lth.se/help/R/strange.RData"))
}

# First glance at data...
foo(a)
foo(c)

## $name
## [1] "a"
##
## $storage.mode
## [1] "integer"
##
## $na
## [1] FALSE
##
## $nan
## [1] FALSE
##
## $inf
## [1] FALSE
##
## $ok
## [1] TRUE
##
## [1] 15000
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
##    41.0    51.0    63.0   292.2   111.0 65170.0
## $name
## [1] "c"
##
## $storage.mode
## [1] "double"
##
## $na
## [1] FALSE
##
## $nan
## [1] FALSE
##
## $inf
## [1] FALSE
##
## $ok
## [1] TRUE
##
## [1] 1
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
##   53.43   53.43   53.43   53.43   53.43   53.43
##

# But, trying the following, will result in
# no-reproducible error messages. Sometimes
# it errors at kk==1, sometimes at kk >> 1.
# Also, look at the different output for
# different kk:s.
for (kk in 1:100) {
   cat("kk=",kk, "\n")
   print(summary(a-c))
}

## kk= 1
## 	  Min.     1st Qu.      Median        Mean     3rd Qu.        Max.
## -7.741e+307  -2.431e+00   9.569e+00               5.757e+01
## kk= 2
## 	Min.   1st Qu.    Median      Mean   3rd Qu.      Max.
##   -12.430    -2.431     9.569   238.700    57.570 65120.000
## kk= 3
## 	Min.   1st Qu.    Median      Mean   3rd Qu.      Max.
##   -12.430    -2.431     9.569              57.570 65120.000
## kk= 4
## 	Min.   1st Qu.    Median      Mean   3rd Qu.      Max.
##   -12.430    -2.431     9.569   238.700    57.570 65120.000
## kk= 5
## 	Min.   1st Qu.    Median      Mean   3rd Qu.      Max.
##   -12.430    -2.431     9.569   238.700    57.570 65120.000
## kk= 6
## Error in quantile.default(object) : missing values and NaN's
## not allowed if 'na.rm' is FALSE


## Comments: If you shorten down 'a', the bug occurs less frequently.



[Rd] Memory leakage/violation?
Henrik Bengtsson