From cstrato at aon.at  Sun Feb  4 21:20:26 2007
From: cstrato at aon.at (cstrato)
Date: Sun, 04 Feb 2007 21:20:26 +0100
Subject: [Rd] Problem using ofstream in C++ class in package for MacOS X
In-Reply-To: <17862.14169.654577.51179@basebud.nulle.part>
References: <45C6315F.9020209@aon.at>
	<17862.14169.654577.51179@basebud.nulle.part>
Message-ID: <45C6400A.3050700@aon.at>

Dirk Eddelbuettel wrote:
> 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
>  
>   
Dear Dirk

Thank you for your fast answer.
Sorrowly, I don?t know how to use a debugger on MacOS X, I am using 
old-style print commands.
However, here is the output of the crash log from my Mac. Does it help?

**********

Host Name:      coeurebooks-computer
Date/Time:      2007-02-04 18:51:50.233 +0100
OS Version:     10.4.8 (Build 8N1051)
Report Version: 4

Command: R
Path:    /Library/Frameworks/R.framework/Resources/bin/exec/i386/R
Parent:  bash [206]

Version: ??? (???)

PID:    1836
Thread: 0

Exception:  EXC_BAD_ACCESS (0x0001)
Codes:      KERN_PROTECTION_FAILURE (0x0002) at 0x00000006

Thread 0 Crashed:
0   libstdc++.6.dylib     0x020fe231 std::basic_ostream<char, 
std::char_traits<char> >::flush() + 17 (ostream.tcc:395)
1   libstdc++.6.dylib     0x020fe358 std::basic_ostream<char, 
std::char_traits<char> 
 >::sentry::sentry[in-charge](std::basic_ostream<char, 
std::char_traits<char> >&) + 120 (ostream.tcc:56)
2   libstdc++.6.dylib     0x02100b5d std::basic_ostream<char, 
std::char_traits<char> >& std::operator<< <std::char_traits<char> 
 >(std::basic_ostream<char, std::char_traits<char> >&, char const*) + 29 
(ostream.tcc:620)
3   MyClass.so            0x0004a30f MyClassA::WriteFileCpp(char const*) 
+ 335
4   MyClass.so            0x0004a6d0 WriteFileCpp + 80
5   libR.dylib            0x0106c701 do_dotCode + 7789 (dotcode.c:1732)
6   libR.dylib            0x01096a0f Rf_eval + 1530 (eval.c:445)
7   libR.dylib            0x01098b43 do_set + 561 (eval.c:1357)
8   libR.dylib            0x010968b4 Rf_eval + 1183 (eval.c:431)
9   libR.dylib            0x01098be1 do_begin + 62 (eval.c:1108)
10  libR.dylib            0x010968b4 Rf_eval + 1183 (eval.c:431)
11  libR.dylib            0x01099fd6 Rf_applyClosure + 688 (eval.c:614)
12  libR.dylib            0x01096725 Rf_eval + 784 (eval.c:455)
13  libR.dylib            0x010b458c Rf_ReplIteration + 396 (main.c:256)
14  libR.dylib            0x010b48f3 R_ReplConsole + 148 (main.c:306)
15  libR.dylib            0x010b4c30 run_Rmainloop + 91 (main.c:945)
16  libR.dylib            0x010b4c4b Rf_mainloop + 16 (main.c:952)
17  R                     0x00001f64 main + 54 (Rmain.c:35)
18  R                     0x00001f12 _start + 216
19  R                     0x00001e39 start + 41

Thread 0 crashed with X86 Thread State (32-bit):
  eax: 0x00000012    ebx: 0x0004a1cb ecx: 0x0214f178 edx: 0x00000000
  edi: 0xbfffe568    esi: 0x0214f178 ebp: 0xbfffe508 esp: 0xbfffe4f0
   ss: 0x0000001f    efl: 0x00010286 eip: 0x020fe231  cs: 0x00000017
   ds: 0x0000001f     es: 0x0000001f  fs: 0x00000000  gs: 0x00000037

Binary Images Description:
    0x1000 -     0x1fff R     
/Library/Frameworks/R.framework/Resources/bin/exec/i386/R
    0x5000 -     0x6fff libRblas.dylib     
/Library/Frameworks/R.framework/Resources/lib/i386/libRblas.dylib
    0xa000 -    0x11fff libgcc_s.1.0.dylib     
/Library/Frameworks/R.framework/Versions/2.4/Resources/lib/libgcc_s.1.0.dylib
   0x49000 -    0x4afff MyClass.so     
/Users/rabbitus/Library/R/library/MyClass/libs/i386/MyClass.so
   0x53000 -    0x70fff libreadline.5.1.dylib     
/Library/Frameworks/R.framework/Versions/2.4/Resources/lib/libreadline.5.1.dylib
  0x582000 -   0x5cffff libgfortran.0.dylib     
/Library/Frameworks/R.framework/Versions/2.4/Resources/lib/libgfortran.0.dylib
  0x712000 -   0x716fff methods.so     
/Library/Frameworks/R.framework/Resources/library/methods/libs/i386/methods.so
 0x1008000 -  0x11f5fff libR.dylib     
/Library/Frameworks/R.framework/Resources/lib/i386/libR.dylib
 0x1705000 -  0x171ffff grDevices.so     
/Library/Frameworks/R.framework/Resources/library/grDevices/libs/i386/grDevices.so
 0x17d1000 -  0x17d8fff libgcc_s.1.0.dylib     
/usr/local/gcc4.0/lib/libgcc_s.1.0.dylib
 0x2008000 -  0x204ffff stats.so     
/Library/Frameworks/R.framework/Resources/library/stats/libs/i386/stats.so
 0x20bc000 -  0x214dfff libstdc++.6.dylib     
/usr/local/gcc4.0/lib/libstdc++.6.dylib
0x8fe00000 - 0x8fe49fff dyld 46.9    /usr/lib/dyld
0x90000000 - 0x9016ffff libSystem.B.dylib     /usr/lib/libSystem.B.dylib
0x901bf000 - 0x901c1fff libmathCommon.A.dylib     
/usr/lib/system/libmathCommon.A.dylib
0x901c3000 - 0x90200fff com.apple.CoreText 1.1.1 (???)    
/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/CoreText.framework/Versions/A/CoreText
0x90227000 - 0x902fcfff ATS     
/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/ATS.framework/Versions/A/ATS
0x9031c000 - 0x90771fff com.apple.CoreGraphics 1.258.51 (???)    
/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/CoreGraphics.framework/Versions/A/CoreGraphics
0x90808000 - 0x908d0fff com.apple.CoreFoundation 6.4.6 (368.27)    
/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation
0x9090e000 - 0x9090efff com.apple.CoreServices 10.4 (???)    
/System/Library/Frameworks/CoreServices.framework/Versions/A/CoreServices
0x90910000 - 0x90a03fff libicucore.A.dylib     /usr/lib/libicucore.A.dylib
0x90a53000 - 0x90ad2fff libobjc.A.dylib     /usr/lib/libobjc.A.dylib
0x90afb000 - 0x90b5ffff libstdc++.6.dylib     /usr/lib/libstdc++.6.dylib
0x90bce000 - 0x90bd5fff libgcc_s.1.dylib     /usr/lib/libgcc_s.1.dylib
0x90bda000 - 0x90c4dfff com.apple.framework.IOKit 1.4.6 (???)    
/System/Library/Frameworks/IOKit.framework/Versions/A/IOKit
0x90c62000 - 0x90c74fff libauto.dylib     /usr/lib/libauto.dylib
0x90c7a000 - 0x90f20fff com.apple.CoreServices.CarbonCore 682.15    
/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/CarbonCore.framework/Versions/A/CarbonCore
0x90f63000 - 0x90fcbfff com.apple.CoreServices.OSServices 4.1    
/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/OSServices.framework/Versions/A/OSServices
0x91004000 - 0x91042fff com.apple.CFNetwork 129.18    
/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/CFNetwork.framework/Versions/A/CFNetwork
0x91055000 - 0x91065fff com.apple.WebServices 1.1.3 (1.1.0)    
/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/WebServicesCore.framework/Versions/A/WebServicesCore
0x91070000 - 0x910effff com.apple.SearchKit 1.0.5    
/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/SearchKit.framework/Versions/A/SearchKit
0x91129000 - 0x91147fff com.apple.Metadata 10.4.4 (121.36)    
/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/Metadata.framework/Versions/A/Metadata
0x91153000 - 0x91161fff libz.1.dylib     /usr/lib/libz.1.dylib
0x91164000 - 0x91303fff com.apple.security 4.4.1 (27569)    
/System/Library/Frameworks/Security.framework/Versions/A/Security
0x91401000 - 0x91409fff com.apple.DiskArbitration 2.1.1    
/System/Library/Frameworks/DiskArbitration.framework/Versions/A/DiskArbitration
0x91410000 - 0x91436fff com.apple.SystemConfiguration 1.8.6    
/System/Library/Frameworks/SystemConfiguration.framework/Versions/A/SystemConfiguration
0x91448000 - 0x9144ffff libbsm.dylib     /usr/lib/libbsm.dylib
0x91453000 - 0x914c9fff com.apple.audio.CoreAudio 3.0.4    
/System/Library/Frameworks/CoreAudio.framework/Versions/A/CoreAudio
0x9151a000 - 0x9151afff com.apple.ApplicationServices 10.4 (???)    
/System/Library/Frameworks/ApplicationServices.framework/Versions/A/ApplicationServices
0x9151c000 - 0x91548fff com.apple.AE 314 (313)    
/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/AE.framework/Versions/A/AE
0x9155b000 - 0x9162ffff com.apple.ColorSync 4.4.6    
/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/ColorSync.framework/Versions/A/ColorSync
0x9166a000 - 0x916ddfff com.apple.print.framework.PrintCore 4.6 
(177.13)    
/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/PrintCore.framework/Versions/A/PrintCore
0x9170b000 - 0x917b4fff com.apple.QD 3.10.21 (???)    
/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/QD.framework/Versions/A/QD
0x917da000 - 0x91825fff com.apple.HIServices 1.5.2 (???)    
/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/HIServices.framework/Versions/A/HIServices
0x91844000 - 0x9185afff com.apple.LangAnalysis 1.6.3    
/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/LangAnalysis.framework/Versions/A/LangAnalysis
0x91866000 - 0x91881fff com.apple.FindByContent 1.5    
/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/FindByContent.framework/Versions/A/FindByContent
0x9188c000 - 0x918c9fff com.apple.LaunchServices 181    
/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/LaunchServices
0x918dd000 - 0x918e9fff com.apple.speech.synthesis.framework 3.5    
/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/SpeechSynthesis.framework/Versions/A/SpeechSynthesis
0x918f0000 - 0x9192bfff com.apple.ImageIO.framework 1.5.0    
/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/ImageIO.framework/Versions/A/ImageIO
0x9193d000 - 0x919effff libcrypto.0.9.7.dylib     
/usr/lib/libcrypto.0.9.7.dylib
0x91a35000 - 0x91a4bfff libcups.2.dylib     /usr/lib/libcups.2.dylib
0x91a50000 - 0x91a6efff libJPEG.dylib     
/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/ImageIO.framework/Versions/A/Resources/libJPEG.dylib
0x91a73000 - 0x91ad1fff libJP2.dylib     
/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/ImageIO.framework/Versions/A/Resources/libJP2.dylib
0x91ae3000 - 0x91ae7fff libGIF.dylib     
/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/ImageIO.framework/Versions/A/Resources/libGIF.dylib
0x91ae9000 - 0x91b66fff libRaw.dylib     
/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/ImageIO.framework/Versions/A/Resources/libRaw.dylib
0x91b6a000 - 0x91ba7fff libTIFF.dylib     
/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/ImageIO.framework/Versions/A/Resources/libTIFF.dylib
0x91bad000 - 0x91bc7fff libPng.dylib     
/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/ImageIO.framework/Versions/A/Resources/libPng.dylib
0x91bcc000 - 0x91bcefff libRadiance.dylib     
/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/ImageIO.framework/Versions/A/Resources/libRadiance.dylib
0x91bd0000 - 0x91bd0fff com.apple.Accelerate 1.3.1 (Accelerate 1.3.1)    
/System/Library/Frameworks/Accelerate.framework/Versions/A/Accelerate
0x91bd2000 - 0x91c60fff com.apple.vImage 2.5    
/System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vImage.framework/Versions/A/vImage
0x91c67000 - 0x91c67fff com.apple.Accelerate.vecLib 3.3.1 (vecLib 
3.3.1)    
/System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/vecLib
0x91c69000 - 0x91cc2fff libvMisc.dylib     
/System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libvMisc.dylib
0x91ccb000 - 0x91ceffff libvDSP.dylib     
/System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libvDSP.dylib
0x91cf7000 - 0x92100fff libBLAS.dylib     
/System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
0x9213a000 - 0x924eefff libLAPACK.dylib     
/System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libLAPACK.dylib
0x9251b000 - 0x92598fff com.apple.DesktopServices 1.3.4    
/System/Library/PrivateFrameworks/DesktopServicesPriv.framework/Versions/A/DesktopServicesPriv
0x925d9000 - 0x92809fff com.apple.Foundation 6.4.7 (567.28)    
/System/Library/Frameworks/Foundation.framework/Versions/C/Foundation
0x92915000 - 0x929f3fff libxml2.2.dylib     /usr/lib/libxml2.2.dylib
0x92a10000 - 0x92afdfff libiconv.2.dylib     /usr/lib/libiconv.2.dylib
0x92b0d000 - 0x92b24fff libGL.dylib     
/System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libGL.dylib
0x92b2f000 - 0x92b87fff libGLU.dylib     
/System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libGLU.dylib
0x92bbc000 - 0x92bc4fff com.apple.speech.recognition.framework 3.6    
/System/Library/Frameworks/Carbon.framework/Versions/A/Frameworks/SpeechRecognition.framework/Versions/A/SpeechRecognition
0x92db0000 - 0x92dbefff com.apple.audio.SoundManager 3.9.1    
/System/Library/Frameworks/Carbon.framework/Versions/A/Frameworks/CarbonSound.framework/Versions/A/CarbonSound
0x92dcf000 - 0x930c3fff com.apple.HIToolbox 1.4.8 (???)    
/System/Library/Frameworks/Carbon.framework/Versions/A/Frameworks/HIToolbox.framework/Versions/A/HIToolbox
0x931c9000 - 0x931d4fff com.apple.opengl 1.4.13    
/System/Library/Frameworks/OpenGL.framework/Versions/A/OpenGL
0x93266000 - 0x9391cfff com.apple.AppKit 6.4.8 (824.42)    
/System/Library/Frameworks/AppKit.framework/Versions/C/AppKit
0x93c9d000 - 0x93d16fff com.apple.CoreData 90    
/System/Library/Frameworks/CoreData.framework/Versions/A/CoreData
0x93d4f000 - 0x93e07fff com.apple.audio.toolbox.AudioToolbox 1.4.3    
/System/Library/Frameworks/AudioToolbox.framework/Versions/A/AudioToolbox
0x93e49000 - 0x93e49fff com.apple.audio.units.AudioUnit 1.4.2    
/System/Library/Frameworks/AudioUnit.framework/Versions/A/AudioUnit
0x93e4b000 - 0x9401dfff com.apple.QuartzCore 1.4.10    
/System/Library/Frameworks/QuartzCore.framework/Versions/A/QuartzCore
0x9406e000 - 0x940affff libsqlite3.0.dylib     /usr/lib/libsqlite3.0.dylib
0x940b7000 - 0x940f1fff libGLImage.dylib     
/System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libGLImage.dylib
0x95fba000 - 0x95fe8fff libncurses.5.4.dylib     
/usr/lib/libncurses.5.4.dylib
0x96a29000 - 0x96a29fff com.apple.vecLib 3.3.1 (vecLib 3.3.1)    
/System/Library/Frameworks/vecLib.framework/Versions/A/vecLib
0x9b9f1000 - 0x9b9f1fff libmx.A.dylib     /usr/lib/libmx.A.dylib

Best regards
Christian



[Rd] Problem using ofstream in C++ class in package for MacOS X
cstrato