[Rd] C/C++ 'assert' should not be used in R packages

Duncan Murdoch murdoch at stats.uwo.ca
Sat Nov 10 18:27:24 CET 2007


On 10/11/2007 6:38 AM, Duncan Murdoch wrote:
> Prof Brian Ripley wrote:
>> Please don't use 'assert' in R packages.  If called, this means that an 
>> error in your code aborts the whole R process, including your user's work. 
>> I see several R packages doing this, and one of them called 'assert' on me 
>> earlier in the week.
>>   
> I partly disagree about this.  If assert() is triggered, it clearly 
> indicates a bug in the package.  If it just generated an R error, most 
> users would ignore it, and not report it to the package maintainer. 
> 
> It may well be that when an assertion fails, none of the subsequent 
> calculations are reliable, in which case returning control to the user 
> could result in data corruption.  That's worse than losing a session, 
> because at least when you lose a session, you know it.
> 
> Could we write our own implementation of assert() that displays an R 
> error and unloads the package?  I think I could do something like that 
> in Windows by calling FreeLibrary to unload the DLL, but I'd prefer a 
> cross-platform solution.

I've put an implementation of this into rgl (svn only for now, not on 
CRAN).  I didn't bother with the FreeLibrary call, the error message 
asks the user to inform the maintainer.

It won't help with the Mesa GL problem below, as that's not rgl code, 
but at least now I won't feel bad about including assert()'s in my code.

Duncan Murdoch

>> We provide 'error': please do use it to return control to the user when 
>> your code misbehaves.
>>
>> Similarly 'exit' and 'abort' should never be used in R packages.
>>
>>
>> Sometimes it is not under your control: I sometimes see an rgl failure at
>>
>> R: indirect_vertex_array.c:659: emit_DrawArrays_old: Assertion 
>> `elements_per_request >= count' failed.
>>
>> that is coming from the Mesa GL libraries.
>>   
> I'd say that's a bug, either in Mesa GL or in rgl.  If you can make it 
> reproducible, I'll try to track it down.
> 
> Duncan Murdoch
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



More information about the R-devel mailing list