Skip to content

[Bioc-devel] vignette problems

2 messages · Martin Morgan

#
On 04/01/2018 03:53 PM, campos wrote:
Please keep the conversation on the bioc-devel mailing list, so that 
others can learn or help.

I use Linux, not Mac, but C (which I used to mean your C++ code) errors 
often occur on all platforms but are only visible as a segfault on one. 
I created the vignette R code with

   cd vignettes
   R CMD Stangle STAN-knitr.Rmd

This produces a file STAN-knitr.R. I then ran your R code with valgrind

   R -d valgrind -f STAN-knitr.R

this runs much slower than without valgrind. The first error reported by 
valgrind was


 > ## 
----STAN-PoiLog-----------------------------------------------------------
 > nStates = 10
 > hmm_poilog = initHMM(trainRegions, nStates, "PoissonLogNormal", 
sizeFactors)
 > hmm_fitted_poilog = fitHMM(trainRegions, hmm_poilog, 
sizeFactors=sizeFactors, maxIters=10)
[1] 6
==22304== Invalid write of size 4
==22304==    at 0x4B489316: HMM::BaumWelch[abi:cxx11](double***, int*, 
int, int, int**, int*, int*, int*, int, int, int**, double***, SEXPREC*, 
SEXPREC*, int, double, double, int, int) (HMM.cpp:998)
==22304==    by 0x4B4A0EFF: RHMMFit (RWrapper.cpp:1494)
==22304==    by 0x4F2992D: R_doDotCall (dotcode.c:692)
==22304==    by 0x4F339D5: do_dotcall (dotcode.c:1252)
==22304==    by 0x4F81BA6: bcEval (eval.c:6771)
==22304==    by 0x4F6E963: Rf_eval (eval.c:624)
==22304==    by 0x4F71188: R_execClosure (eval.c:1764)
==22304==    by 0x4F70E7C: Rf_applyClosure (eval.c:1692)
==22304==    by 0x4F6F18B: Rf_eval (eval.c:747)
==22304==    by 0x4F74B12: do_set (eval.c:2774)
==22304==    by 0x4F6EDF5: Rf_eval (eval.c:699)
==22304==    by 0x4FB7BEE: Rf_ReplIteration (main.c:258)
==22304==  Address 0x238b28f4 is 4 bytes inside a block of size 5 alloc'd
==22304==    at 0x4C2DB8F: malloc (in 
/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==22304==    by 0x4B4892E5: HMM::BaumWelch[abi:cxx11](double***, int*, 
int, int, int**, int*, int*, int*, int, int, int**, double***, SEXPREC*, 
SEXPREC*, int, double, double, int, int) (HMM.cpp:995)
==22304==    by 0x4B4A0EFF: RHMMFit (RWrapper.cpp:1494)
==22304==    by 0x4F2992D: R_doDotCall (dotcode.c:692)
==22304==    by 0x4F339D5: do_dotcall (dotcode.c:1252)
==22304==    by 0x4F81BA6: bcEval (eval.c:6771)
==22304==    by 0x4F6E963: Rf_eval (eval.c:624)
==22304==    by 0x4F71188: R_execClosure (eval.c:1764)
==22304==    by 0x4F70E7C: Rf_applyClosure (eval.c:1692)
==22304==    by 0x4F6F18B: Rf_eval (eval.c:747)
==22304==    by 0x4F74B12: do_set (eval.c:2774)
==22304==    by 0x4F6EDF5: Rf_eval (eval.c:699)

'Invalid write' suggests that you are writing after the end of memory 
that you'd allocated. I looked at the C code at the line where the error 
occurs as indicated in the stack trace, HMM.cpp:998 which is the 
assigment myStateBucks[i] = 0 in the loop

     int *myStateBuckets = (int*)malloc(sizeof(int)*ncores+1);
     for(i=0; i<=ncores; i++)
     {
         myStateBuckets[i] = 0;
     }

The argument to malloc (where he memory was allocated, at line 995) 
should be the number of bytes to allocate and it should have been memory 
for ncores + 1 'int'

   malloc(sizeof(int) * (ncores + 1))

rather than what you wrote, which is memory for ncores ints plus 1 byte.

C++ code would avoid the need for such explicit memory management, e.g., 
using a vector from the standard template library

   std::vector<int> myStateBuckets(ncores);

There were may other valgrind errors, but I do not know whether these 
are from similar programming errors, or a consequence of this one.

Martin
This email message may contain legally privileged and/or...{{dropped:2}}
#
On 04/01/2018 08:06 PM, Martin Morgan wrote:
oops, std::vector<int> myStateBuckets(ncores + 1); !
This email message may contain legally privileged and/or...{{dropped:2}}