J
james.lawton
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<istream> istream_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<T> const & );
ptr<T> & operator=( ptr<T> const & );
//void swap( ptr<T> & other ) { std::swap( _ptr, other._ptr ); }
T * _ptr;
};
typedef ptr<istream> istream_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 ----------
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<istream> istream_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<T> const & );
ptr<T> & operator=( ptr<T> const & );
//void swap( ptr<T> & other ) { std::swap( _ptr, other._ptr ); }
T * _ptr;
};
typedef ptr<istream> istream_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 ----------