Boost logo

Boost Users :

Subject: Re: [Boost-users] shared_ptr question
From: David Michael Bond (david.bond_at_[hidden])
Date: 2010-05-18 09:23:39


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

-David

 

From: boost-users-bounces_at_[hidden]
[mailto:boost-users-bounces_at_[hidden]] On Behalf Of
richard.carless_at_[hidden]
Sent: Tuesday, May 18, 2010 9:22 AM
To: boost-users_at_[hidden]
Subject: Re: [Boost-users] shared_ptr question

 

are you running these functions on different threads?

Internet
david.bond_at_[hidden]

Sent by: boost-users-bounces_at_[hidden]

18/05/2010 05:00

Please respond to
boost-users_at_[hidden]

To

Boost-users_at_[hidden]

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_at_[hidden]
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-8
45C5H for additional disclosures.



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