Re: [Boost-users] shared_ptr question
I'm not sure but can you test it following way: Defer this declaration " boost::shared_ptr<Frame> rx" in following way and see if that helps ... boost::shared_ptr<Frame> &rx = processing_queue.back( ) ; Regards, ~ Soumen On Tue, May 18, 2010 at 9:30 AM, David Michael Bond <david.bond@iol.unh.edu>wrote:
Hello, I am learning shared_ptrs and I am running into a segfault. I'm fairly certain it's because of some misunderstanding I have.
Here is some code. I have altered it to just so the relevant data:
---- START CODE ---- std::deque<boost::shared_ptr<Frame> > processing_queue ; void this_is_called() { boost::shared_ptr<Frame> rx ; interface->open( "eth0" ) ; while( running ) { interface->rx_frame( this->processing_queue ) ; if( !processing_queue.empty( ) ) { rx = processing_queue.back( ) ; if( rx ) { std::cout << "Received Frame of Ethertype " << ntohs( rx->get_ether_header( ).ether_type ) << "." << std::endl ; } } } } }
// rx frame from interface and add onto processing_queue boost::shared_ptr<Frame> last_rx_frame ; // (CLASSXX member) void CLASSXX::rx_frame( std::deque<boost::shared_ptr<Frame> >& processing_queue ) { // Do some stuff to receive frame.. (int size= # bytes in frame) last_rx_frame.reset(new Frame( )) ; last_rx_frame->bytes.reset( new u_char[size]) ; memcpy( last_rx_frame->bytes.get(), frame_bytes, size) ; last_rx_frame->size = size ; processing_queue.push_back( this->last_rx_frame ) ; }
---- END CODE ----
The program receives one frame and successful and outputs it: Received Frame of Ethertype 0x800. But the second time when it gets to 'rx = processing_queue.back( ) ;' I have a segfault.
My understanding of shared_ptrs is that when this assignment is done rx will have its ref count decremented (possibly leading to it being deleted). The back of the processing_queue will then become rx do to the assignment and that shared ptrs reference count will be increased by one. Anyone know what my misconception is? Any idea what's causing the segfault?
Thanks! -David
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Defer this declaration " boost::shared_ptr<Frame> rx" in following way and see if that helps ... boost::shared_ptr<Frame> &rx = processing_queue.back( ) ;
This prevents the segfault, but I'm very curious about what was causing it. Any insight on the fundamental misuse?
Why not just assign? last_rx_frame = new Frame;
Last_rx_frame is a shared_ptr. That assignment is invalid. I have done it without a reset (creating and assigning a new shared_ptr and that has no effect.
last_rx_frame->bytes.reset( new u_char[size]) ; Is last_rx_frame->bytes also a shared_ptr? According to boost docs, a shared_ptr shouldn't be used to hold a dynamically allocated array; you should use shared_array instead. Just a shot in the dark.
last_rx_frame->bytes is a shared_array. Thanks for the help so far guys! I looked into it more and it seems if I comment out the memcpy in the get_ether_header function I do not get the segfault. ether_header Frame::get_ether_header( ) { ether_header ret ; memcpy( &ret, this->bytes.get(), this->size ) ; return ret ; } Its fine to return that stack var right there right? That should just copy the memory into the return value for the stack? -David
participants (2)
-
David Michael Bond
-
Soumen Ghosh