473,396 Members | 1,792 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

auto_ptr<istream> problem

Hi,

I'm having a problem that I can't diagnose. I'm creating istreams of
unknown types, and want to manage them on the stack, co I'm passing
around ownership-holding pointers. Usually, I would use
std::auto_ptr<std::istream>, but it seems to be deallocating early, as
the call to read(...) below breaks.

I've condensed a test case. Using my own pointer works fine, but using
auto_ptr does not (see USE_AUTO_PTR). I solved the issue by a little
trial and error, but I don't understand the cause.

Help would be greatly appreciated. I apologise if I'm simply being
idiotic, but I've been trying to work this out for days.

I'm compiling under Visual Studio 2005 (cl.exe version 14.00.50727.42)

-- James
// ---------- Begin ptr_test.cpp ----------

#include <algorithm>
#include <istream>
#include <fstream>
#include <memory>
using namespace std;

// Ownership transfering pointer to input
stream //////////////////////

//#define USE_AUTO_PTR

#if defined(USE_AUTO_PTR)
typedef auto_ptr<istreamistream_ptr;
#else

/* Not using copy constructor in example, so I won't bother writing
* a standards compliant one here. Nor assignment operator. Instead
* I'll make them private to be sure they're not generated.
*/
template <class T>
struct ptr {
ptr( T * ptr ) : _ptr( ptr ) {}
//ptr( ptr<T& other ) : _ptr( 0 ) { swap( other ); }
~ptr() { if ( _ptr ) delete _ptr; }
T * operator->() const { return _ptr; }
private:
ptr( ptr<Tconst & );
ptr<T& operator=( ptr<Tconst & );
//void swap( ptr<T& other ) { std::swap( _ptr, other._ptr ); }
T * _ptr;
};
typedef ptr<istreamistream_ptr;

#endif

// Simple test
case ///////////////////////////////////////////////////

int main() {
istream_ptr in = new fstream( "ptr_test.cpp", ios::binary );
char ch;
in->read( &ch, 1 );
return 0;
}

// ---------- End ptr_test.cpp ----------
Jun 27 '08 #1
4 2349
ja**********@gmail.com wrote:
Hi,

I'm having a problem that I can't diagnose. I'm creating istreams of
unknown types, and want to manage them on the stack, co I'm passing
around ownership-holding pointers. Usually, I would use
std::auto_ptr<std::istream>, but it seems to be deallocating early, as
the call to read(...) below breaks.

I've condensed a test case. Using my own pointer works fine, but using
auto_ptr does not (see USE_AUTO_PTR). I solved the issue by a little
trial and error, but I don't understand the cause.
Your program below doesn't compile here on my compiler (and I wouldn't
expect it to), neither with the #define, nor without.
Help would be greatly appreciated. I apologise if I'm simply being
idiotic, but I've been trying to work this out for days.

I'm compiling under Visual Studio 2005 (cl.exe version 14.00.50727.42)

-- James
// ---------- Begin ptr_test.cpp ----------

#include <algorithm>
#include <istream>
#include <fstream>
#include <memory>
using namespace std;

// Ownership transfering pointer to input
stream //////////////////////

//#define USE_AUTO_PTR

#if defined(USE_AUTO_PTR)
typedef auto_ptr<istreamistream_ptr;
#else

/* Not using copy constructor in example, so I won't bother writing
* a standards compliant one here. Nor assignment operator. Instead
* I'll make them private to be sure they're not generated.
*/
template <class T>
struct ptr {
ptr( T * ptr ) : _ptr( ptr ) {}
//ptr( ptr<T& other ) : _ptr( 0 ) { swap( other ); }
~ptr() { if ( _ptr ) delete _ptr; }
T * operator->() const { return _ptr; }
private:
ptr( ptr<Tconst & );
ptr<T& operator=( ptr<Tconst & );
//void swap( ptr<T& other ) { std::swap( _ptr, other._ptr ); }
T * _ptr;
};
typedef ptr<istreamistream_ptr;

#endif

// Simple test
case ///////////////////////////////////////////////////

int main() {
istream_ptr in = new fstream( "ptr_test.cpp", ios::binary );
Try:

istream_ptr in(new fstream( "ptr_test.cpp", ios::binary ));

That avoids creating a temporary, for which a copy constructor would be
needed that takes a const istream_ptr as argument. std::auto_ptr doesn't
have that (and neither does your class, so it shouldn't compile either).
char ch;
in->read( &ch, 1 );
return 0;
}

// ---------- End ptr_test.cpp ----------
Jun 27 '08 #2
* * * * istream_ptr in = new fstream( "ptr_test.cpp", ios::binary );

Visual Studio 2005 has a buggy implementation of std::auto_ptr where
this code compiles, but has the side effect that either the memory is
deleted twice or that the memory is deleted directly after that
statement, I don't remember.

Write this instead:

istream_ptr in(new fstream( "ptr_test.cpp", ios::binary ));

Regards,
Anders Dalvander
Jun 27 '08 #3
On 23 Jun, 03:51, Rolf Magnus <ramag...@t-online.dewrote:
Your program below doesn't compile here on my compiler (and I wouldn't
expect it to), neither with the #define, nor without.
Thank you for pointing it out. In fact, I hadn't explicitly turned off
the "language extensions" in Visual Studio, and it was optimising away
the copy constructor without so much as a warning for my pointer
class.

For auto_ptr, it implicitly converted to auto_ptr_ref, then
constructed from that, which is fair enough. In fact, my actual
pointer class does have an implicit conversion and pseudo-copy-
constructor similar to auto_ptr, so that wasn't my problem.

I found my problem in the full program, and I show it below in the
function loadResource. Rather than (as I hoped) using
auto_ptr<istream>::auto_ptr( istream * ), the return statement is in
fact calling auto_ptr_ref<istream>::auto_ptr_ref( void * ) then
auto_ptr( auto_ptr_ref<istream& ). The pointer stored in the
auto_ptr_ref is statically cast to (auto_ptr<istream*) when it is
really a (ifstream *) at heart. Cue problems.

istream_ptr loadResource( string const & name ) {
return new fstream( name.c_str(), ios::binary );
// Above line should be the following
//return istream_ptr( new fstream( name.c_str(), ios::binary ) );
}

int main() {
istream_ptr in = loadResource( "ptr_test.cpp" );
char ch;
in->read( &ch, 1 );
return 0;
}

// ---------- Begin ptr_test.cpp ----------
#include <algorithm>
#include <istream>
#include <fstream>
#include <memory>
using namespace std;
// Ownership transfering pointer to input
stream //////////////////////
//#define USE_AUTO_PTR
#if defined(USE_AUTO_PTR)
typedef auto_ptr<istreamistream_ptr;
#else
/* Not using copy constructor in example, so I won't bother writing
** a standards compliant one here. Nor assignment operator. Instead
** I'll make them private to be sure they're not generated.
**/
template <class T>
struct ptr {
ptr( T * ptr ) : _ptr( ptr ) {}
//ptr( ptr<T& other ) : _ptr( 0 ) { swap( other ); }
~ptr() { if ( _ptr ) delete _ptr; }
T * operator->() const { return _ptr; }
private:
ptr( ptr<Tconst & );
ptr<T& operator=( ptr<Tconst & );
//void swap( ptr<T& other ) { std::swap( _ptr, other._ptr ); }
T * _ptr;
};
typedef ptr<istreamistream_ptr;
#endif
// Simple test
case ///////////////////////////////////////////////////
int main() {
istream_ptr in = new fstream( "ptr_test.cpp", ios::binary );

Try:

istream_ptr in(new fstream( "ptr_test.cpp", ios::binary ));

That avoids creating a temporary, for which a copy constructor would be
needed that takes a const istream_ptr as argument. std::auto_ptr doesn't
have that (and neither does your class, so it shouldn't compile either).
char ch;
in->read( &ch, 1 );
return 0;
}
// ---------- End ptr_test.cpp ----------
Jun 27 '08 #4
On 23 Jun, 13:32, Anders Dalvander <goo...@dalvander.comwrote:
* * * * istream_ptr in = new fstream( "ptr_test.cpp", ios::binary );

Visual Studio 2005 has a buggy implementation of std::auto_ptr where
this code compiles, but has the side effect that either the memory is
deleted twice or that the memory is deleted directly after that
statement, I don't remember.

Write this instead:

istream_ptr in(new fstream( "ptr_test.cpp", ios::binary ));

Regards,
Anders Dalvander
Thank you, Anders. I managed to discover this (eventually) and just
posted my description of it before I saw this post. I think this
should be a warning to anyone who wants an object to construct either
from a pointer (especially in a template) or an object with an
implicit conversion from (void *)
Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Bruce Davis | last post by:
I'm having a problem on windows (both 2000 and XP) with a multi-threaded tkinter gui application. The problem appears to be a deadlock condition when a child thread pops up a Pmw dialog window in...
4
by: David Morgan | last post by:
Hi This is a really weird problem. We have a website that has been running for about six months with no problems. We have recently moved it to a new server with a new ISP. The server is of a...
11
by: Kostatus | last post by:
I have a virtual function in a base class, which is then overwritten by a function of the same name in a publically derived class. When I call the function using a pointer to the derived class...
7
by: Keith Dewell | last post by:
Greetings! My current job has brought me back to working in C++ which I haven't used since school days. The solution to my problem may be trivial but I have struggled with it for the last two...
117
by: Peter Olcott | last post by:
www.halting-problem.com
28
by: Jon Davis | last post by:
If I have a class with a virtual method, and a child class that overrides the virtual method, and then I create an instance of the child class AS A base class... BaseClass bc = new ChildClass();...
6
by: Ammar | last post by:
Dear All, I'm facing a small problem. I have a portal web site, that contains articles, for each article, the end user can send a comment about the article. The problem is: I the comment length...
16
by: Dany | last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by...
2
by: Mike Collins | last post by:
I cannot get the correct drop down list value from a drop down I have on my web form. I get the initial value that was loaded in the list. It was asked by someone else what the autopostback was...
9
by: AceKnocks | last post by:
I am working on a framework design problem in which I have to design a C++ based framework capable of solving three puzzles for now but actually it should work with a general puzzle of any kind and I...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.