shared_ptr question
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
participants (1)
-
David Michael Bond