No, but eventually I will be accessing the shared_ptrs from different threads.

-David

 

From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of richard.carless@uk.bnpparibas.com
Sent: Tuesday, May 18, 2010 9:22 AM
To: boost-users@lists.boost.org
Subject: Re: [Boost-users] shared_ptr question

 


are you running these functions on different threads?


Internet  
david.bond@iol.unh.edu

Sent by: boost-users-bounces@lists.boost.org

18/05/2010 05:00

Please respond to
boost-users@lists.boost.org

To

Boost-users@lists.boost.org

cc

Subject

[Boost-users] 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



_______________________________________________
Boost-users mailing list
Boost-users@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

 
___________________________________________________________
This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and delete this e-mail. Any unauthorised copying, disclosure or distribution of the material in this e-mail is prohibited.
 
Please refer to http://www.bnpparibas.co.uk/en/information/legal_information.asp?Code=ECAS-845C5H  for additional disclosures.