From cstrato at aon.at  Mon Feb  5 19:48:17 2007
From: cstrato at aon.at (cstrato)
Date: Mon, 05 Feb 2007 19:48:17 +0100
Subject: [Rd] Problem using ofstream in C++ class in package for MacOS X
In-Reply-To: <182A7E81-FAD6-42C9-8C5A-9D918E416A13@gmail.com>
References: <45C6315F.9020209@aon.at>	<17862.14169.654577.51179@basebud.nulle.part>	<45C6400A.3050700@aon.at>
	<m2d54pob4f.fsf@fhcrc.org> <45C65479.30906@aon.at>
	<182A7E81-FAD6-42C9-8C5A-9D918E416A13@gmail.com>
Message-ID: <45C77BF1.2080106@aon.at>

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
>



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