cstrato
2007-Feb-04 19:17 UTC
[Rd] Problem using ofstream in C++ class in package for MacOS X
Dear all, I am currently learning how to create R packages using C++ classes. For this purpose I have written a small package MyClass (which I try to attach since I do not have access to a website). MyClass has methods WriteFileC() and WriteFileCpp() which implement C-style and C++-style writing of a table to a file using FILE or ofstream respectively, and the corresponding R-functions writeFileC.R and writeFileCpp.R. While I can compile and execute my package w/o problems on Fedora Core 4, I can only execute writeFileC.R on my Intel-MacBook Pro, but not writeFileCpp.R. Executing my functions I get the following output: > library(MyClass) Welcome to MyClass > writeFileC("myout_fileC.txt") [1] "outfile = myout_fileC.txt" Writing file myout_fileC.txt using C style. ---MyClassA::MyClassA()--------- ---MyClassA::WriteFileC--------- <20> records exported. ---MyClassA::~MyClassA()--------- [1] "writeFileC finished" NULL > writeFileCpp("myout_fileCpp.txt") [1] "outfile = myout_fileCpp.txt" Writing file myout_fileCpp.txt using C++ style. ---MyClassA::MyClassA()--------- ---MyClassA::WriteFileCpp--------- *** caught bus error *** address 0x6, cause 'non-existent physical address' Traceback: 1: .C("WriteFileCpp", as.character(outfile), PACKAGE = "MyClass") 2: writeFileCpp("myout_fileCpp.txt") While I understand, that C++ iostreams are best avoided, since there is no guarantee that the output will appear in the R console (as mentioned on page 62 of R-exts.pdf), I do not understand why I cannot use ofstream, which is isolated in a C++ method only. Is there a way how to use ofstream on MacOS X or is this a limit of the current Mac implementation? Thank you in advance. Best regards Christian _._._._._._._._._._._._._._._._ C.h.i.s.t.i.a.n S.t.r.a.t.o.w.a V.i.e.n.n.a A.u.s.t.r.i.a _._._._._._._._._._._._._._._._ -------------- next part -------------- A non-text attachment was scrubbed... Name: MyClass_0.1.2.tar.gz Type: application/x-gzip Size: 9198 bytes Desc: not available Url : https://stat.ethz.ch/pipermail/r-devel/attachments/20070204/2e7fb79c/attachment.gz
Dirk Eddelbuettel
2007-Feb-04 19:43 UTC
[Rd] Problem using ofstream in C++ class in package for MacOS X
On 4 February 2007 at 20:17, cstrato wrote: | I am currently learning how to create R packages using C++ classes. Congratulations :) [ This reminds me to email Oleg (CC'ed). Oleg, your HOWTO is a very useful. Would you consider adding a section on Dominick's most excellent RcppTemplate package which makes writing code to go back and forth between R and C++ soooo much easier? ] | For this purpose I have written a small package MyClass (which I try | to attach since I do not have access to a website). [...] | While I can compile and execute my package w/o problems on Fedora Core 4, Works for me too on Debian. [...] | > writeFileCpp("myout_fileCpp.txt") | [1] "outfile = myout_fileCpp.txt" | Writing file myout_fileCpp.txt using C++ style. | ---MyClassA::MyClassA()--------- | ---MyClassA::WriteFileCpp--------- | | *** caught bus error *** | address 0x6, cause 'non-existent physical address' | | Traceback: | 1: .C("WriteFileCpp", as.character(outfile), PACKAGE = "MyClass") | 2: writeFileCpp("myout_fileCpp.txt") | | | While I understand, that C++ iostreams are best avoided, since there is | no guarantee that the output will appear in the R console (as mentioned | on page 62 of R-exts.pdf), I do not understand why I cannot use ofstream, | which is isolated in a C++ method only. | | Is there a way how to use ofstream on MacOS X or is this a limit of the | current Mac implementation? Can you get to this in a debugger on MacOS X? Just glancing at your code I saw nothing obviously sticking out... Dirk -- Hell, there are no rules here - we're trying to accomplish something. -- Thomas A. Edison
cstrato
2007-Feb-05 18:48 UTC
[Rd] Problem using ofstream in C++ class in package for MacOS X
Dear Jochen Thank you, there seems to be a problem with accessing memory but probably not in my simple program. Here are the essential parts: 1. R-function: "writeFileCpp" <- function(outfile=character(0)) { r <- .C("WriteFileCpp",as.character(outfile), PACKAGE="MyClass"); return(); } 2. R-wrapper: void WriteFileCpp(char **outfile) { MyClassA *classA = new MyClassA(); classA->WriteFileCpp(outfile[0]); delete classA; } 3. C++ method: void MyClassA::WriteFileCpp(const char *outfile) { ofstream output(outfile, ios::out); output << 21 << endl; output.close(); } This crashes with "non-existent physical address" Interestingly, the following C++ method works: void MyClassA::WriteFileCpp(const char *outfile) { ofstream output(outfile, ios::out); // output << 21 << endl; output.close(); } This means, that the operator "<<" seems to cause the problem, but why? Best regards Christian Jochen Laubrock wrote:> Hi cstrato, > > > On 04.02.2007, at 22:47, cstrato wrote: > >> ostream.tcc >> > > > On my system (Mac OS X), ostream.tcc seems to be just a part of the > FSF/GNU implementation of the stl, as > > head -n 40 /usr/include/c++/4.0.0/bits/ostream.tcc > > gives > >> // This file is part of the GNU ISO C++ Library. This library is free >> // software; you can redistribute it and/or modify it under the >> [...] >> /** @file ostream.tcc >> * This is an internal header file, included by other library headers. >> * You should not attempt to use it directly. >> */ >> >> // >> // ISO C++ 14882: 27.6.2 Output streams > > > I think your error looks more like you are accessing invalid memory, > maybe a dangling pointer ? > > hth, jochen >
Possibly Parallel Threads
- Bug: broken exception handling in S4 methods
- [LLVMdev] Get LLVM assembler for a function.
- [LLVMdev] std::cout << *MyModule does not work anymore
- Buildling with/without AddressSanitizer causes divergent execution behaviour
- xapian-compact fails to rename iamflint.tmp for win32/cygwin builds