|
Boost Users : |
From: Robbie Morrison (robbie_at_[hidden])
Date: 2007-10-04 14:28:31
Hi all -- hope someone can help, many TIA -- Robbie
Intro
This relates to the Boost.Smart_ptr library.
The example code (see below) fails when a
boost::shared_ptr is used, but not when a raw pointer
is substituted.
This posting probably highlights some key
misunderstanding on my part. Perhaps someone can
explain where I went wrong.
As a long shot, it could be that the Boost.Ref library
can offer something useful.
Problem
I want to pass in an existing UDT (user-defined type)
by reference to a second UDT during construction. I
figured it would be better to hold this reference in
a smart pointer rather than a raw pointer.
The following stripped-down code mimics this process,
but passes in an int (a fundamental type) instead for
reasons of simplicity.
The example works fine with a raw pointer, but not a
boost::shared_ptr (or std::auto_ptr or
boost::scoped_ptr or std::tr1::shared_ptr).
I checked in Becker (2007) ("C++ standard library
extensions") and Karlsson (2006) ("Beyond the C++
standard library") regarding Boost.Smart_ptr's, but
could not see anything useful.
'frag-pass-by-ref_03.cc' was the name of my program.
Test environment
- Boost release 1.34.1 dated 24-Jul-2007 (source build)
- GNU GCC 4.1.2 compiler (using -Wall -Weffc++ -pedantic)
- Linux Ubuntu 6.10 operating system (now superseded by 7.04)
- run-time memory checking with valgrind version "valgrind-3.2.0-Debian"
- laptop is Toshiba Tecra A2 330, new 2004, 1.4GHz Intel Celeron M
Error messages (normal run-time)
destroying a MyClass
*** glibc detected *** ./frag-pass-by-ref_03: free(): invalid pointer: 0xbfcd12dc ***
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6[0xb7d8a8bd]
/lib/tls/i686/cmov/libc.so.6(__libc_free+0x84)[0xb7d8aa44]
/usr/lib/libstdc++.so.6(_ZdlPv+0x21)[0xb7f3afc1]
./frag-pass-by-ref_03(__gxx_personality_v0+0x3d3)[0x8048baf]
./frag-pass-by-ref_03[0x8048c96]
./frag-pass-by-ref_03(__gxx_personality_v0+0x274)[0x8048a50]
./frag-pass-by-ref_03(__gxx_personality_v0+0x29e)[0x8048a7a]
./frag-pass-by-ref_03(__gxx_personality_v0+0x2ba)[0x8048a96]
./frag-pass-by-ref_03[0x8048cd4]
./frag-pass-by-ref_03(__gxx_personality_v0+0x195)[0x8048971]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xdc)[0xb7d398cc]
./frag-pass-by-ref_03(__gxx_personality_v0+0x65)[0x8048841]
======= Memory map: ========
(remainder omitted)
Error messages (under valgrind)
destroying a MyClass
==23254== Invalid free() / delete / delete[]
==23254== at 0x4020CC7: operator delete(void*) (vg_replace_malloc.c:244)
==23254== by 0x8048BAE: void boost::checked_delete<int>(int*) (checked_delete.hpp:34)
==23254== by 0x8048C95: boost::detail::sp_counted_impl_p<int>::dispose() (sp_counted_impl.hpp:79)
==23254== by 0x8048A4F: boost::detail::sp_counted_base::release() (sp_counted_base_gcc_x86.hpp:145)
==23254== by 0x8048A79: boost::detail::shared_count::~shared_count() (shared_count.hpp:205)
==23254== by 0x8048A95: boost::shared_ptr<int>::~shared_ptr() (shared_ptr.hpp:131)
==23254== by 0x8048CD3: MyClass::~MyClass() (frag-pass-by-ref_03.cc:21)
==23254== by 0x8048970: main (frag-pass-by-ref_03.cc:31)
==23254== Address 0xBE8944FC is on thread 1's stack
(error summary omitted)
8< -------------------------------------------
#include <iostream> // standard io
#include <boost/shared_ptr.hpp> // Boost shared pointer
class MyClass
{
public:
MyClass (int& i) // & indicates pass-by-reference
: d_int(&i) { } // & is address-of operator
~MyClass() { std::cout << "destroying a MyClass" << std::endl; }
private:
boost::shared_ptr<int> d_int; // shared pointer FAILS
// int* d_int; // raw pointer works
};
int main()
{
int myint(2); // simple integer
MyClass myclass(myint); // constructor call
return 0;
}
------------------------------------------- >8
--- Robbie Morrison PhD student -- policy-oriented energy system simulation Institute for Energy Engineering (IET) Technical University of Berlin (TU-Berlin), Germany [from IMAP client]
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net