From Jean-Michel.Perraud at csiro.au  Mon Sep 10 01:51:54 2012
From: Jean-Michel.Perraud at csiro.au (Jean-Michel.Perraud at csiro.au)
Date: Mon, 10 Sep 2012 09:51:54 +1000
Subject: [Rd] Different behavior of the "showArgs" example (R extension
 manual) between gcc and Visual C++ compiled code
Message-ID: <F3AAB43D90409041B7DED35E79F2CF651408119DD4@exvic-mbx05.nexus.csiro.au>

Hi,

I am trying to implement on a Win7 box the showArgs example of section 5.10.2 "Calling .External" of the R extension manual. I am using interchangeably gcc (RTools) and Visual C++ (via Makefile.win) to build a package. I get a couple of runtime oddities when the dll compiled with Visual C++. I'd value comments, observations and tips from interested readers. I tried my best to find proper compilation settings for VCPP, from a variety of source incl. Duncan Murdoch's advice.

Cheers,
J-M

OBSERVATIONS
============
1/ The for() loop does not hit the termination condition on args being R_NilValue, and ends up stuck on pointers as shown below, equality never reached.
		args	0x020e24a0	SEXPREC *
		R_NilValue	0x835425ff	SEXPREC *
2/ If I force it out of the endless loop, after the function returns, I get a nasty "Unhandled exception at 0x6c799419 in Rgui: 0xC0000005: Access violation writing location 0x0009eee9." with a call stack as follow. I did check that the cdecl calling convention is used.
	R.dll!6c799419() 	
 	[Frames below may be incorrect and/or missing, no symbols loaded for R.dll]	
 	R.dll!6c797736() 	
 	user32.dll!7633cd90() 	
 	user32.dll!7636c1c2() 	
 	Rgraphapp.dll!63551cba() 	

CODE
====
// R code
library(showArgs)
blah = as.Date('2000-01-01')
printArgs(a=1:3, b=LETTERS[1:3], blah=blah)
// END R code

// C/Cpp code:
extern "C" { // because I compile the code 'as C++' for Visual C++, for in-code variable declaration
	SEXP __cdecl showArgs_ext(SEXP args);
}

SEXP __cdecl showArgs_ext(SEXP args)
{
// snip
	args = CDR(args); /* skip 'name' */
	for(int i = 0; args != R_NilValue; i++, args = CDR(args)) {
		// verbatim from the manual
	}
	return R_NilValue;
}
// END C/Cpp code


Vcc Compilation settings:
/I"C:\bin\R\R\include" /ZI /nologo /W3 /WX- /Od /Oy- /D "_WINDLL" /D "_MBCS" /Gm /EHsc /RTC1 /GS /fp:precise /Za /Zc:wchar_t /Zc:forScope /Fp"Debug\showArgs.pch" /Fa"Debug\" /Fo"Debug\" /Fd"Debug\vc100.pdb" /Gd /TP /analyze- /errorReport:queue 

/OUT:"C:\XXXX\Debug\showArgs.dll" /NOLOGO /LIBPATH:"C:\bin\R\R\bin\i386" /DLL "Rdll.lib" "kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib" "comdlg32.lib" "advapi32.lib" "shell32.lib" "ole32.lib" "oleaut32.lib" "uuid.lib" "odbc32.lib" "odbccp32.lib" /DEF:"showArgs-win.def" /MANIFEST /ManifestFile:"Debug\showArgs.dll.intermediate.manifest" /ALLOWISOLATION /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /DEBUG /PDB:"C:\XXXX\Debug\showArgs.pdb" /PGD:"C:\XXXX\Debug\showArgs.pgd" /TLBID:1 /DYNAMICBASE /NXCOMPAT /MACHINE:X86 /ERRORREPORT:QUEUE 


ENVIRONMENT
===========
- Windows 7 32 bits
- R version 2.15.0 (2012-03-30) Platform: i386-pc-mingw32/i386 (32-bit)
- RTools 2.15.0
- MS Visual Cpp 2010 (express edition)



[Rd] Different behavior of the "showArgs" example (R extension manual) between gcc and Visual C++ compiled code
Jean-Michel.Perraud at csiro.au