Problem with boost:smart_ptr - invalid pointer?

Started by
16 comments, last by DrEvil 17 years, 6 months ago
No they're fine - when the segmentation violation happens their values are reasonable. It could be KDBG getting fooled by BOOST. It looks like the correct functions are being called but KBDG is maybe showing something wrong.
---------------------http://www.stodge.net
Advertisement
I'm confused. You say you're using Boost.Smart_Ptr. Where? HBMessagePacketPtr clearly isn't a smart pointer since you're explicitly deleting it.

Σnigma
HBMessagePacketPtr is just a typedef for the smart pointer.

The call stack is:

client->tick() // creates packet.
connection->Send(packet) // packet is valid and accessible.
connection->SendTo(packet) // packet is valid and accessible.
protocol->SendTo(packet) // packet appears to be valid in the debugger (ddd) but it is NOT accessible. It segfaults the application.

So in protocol->SendTo(packet), any attempt to use the pointer causes a segfault. Packet can be used/accessed by any function above in the call stack.

I'm so confused.....
---------------------http://www.stodge.net
The whole point of smart pointers is that you never call delete on the pointer they hold. It gets deleted automatically.
I realise that now thanks. But that's irrelevant to this particular problem isn't it? I mean, there are no side effects of me deleting smart pointers.
---------------------http://www.stodge.net
In theory, yes, there are -- you circumvent the natural lifetime management the smart pointer is doing. It is fatal to delete a pointer managed by a smart pointer class.

In practice, HBMessagePacketPtr cannot be a boost::shared_ptr if that code compiles, because you cannot call delete on a boost::shared_ptr. It is not itself a pointer, and it provides no implicit conversion to the underlying pointer object (at least, not in the header I'm looking at).
It's definitely a shared pointer.

I found the problem. If the packet isn't consumed by a protocol layer (connection throttler etc..) then a message queue entry is created and the packet is stored in it. The message queue entry is then added to a message queue to be sent later.

If I completely ignore the queue and just send the packet once I know the protocol layers haven't consumed the packet then the application doesn't segfault.

So my message queue logic is wrong.

Thanks again
---------------------http://www.stodge.net
So if your message queue is storing the packet, presumably by having a shared_ptr to the same object, and you are then calling delete on it? I would guess this leaves the shared_ptr in the message queue in a jacked up state. Don't ever call delete on a shared_ptr, whether thats the problem here or not. Call .reset() if you want to clear a particular shared_ptr.

I'm pretty surprised if that is compiling, it sure doesn't in msvc 2005. Sure you aren't calling delete pack.get()?

This topic is closed to new replies.

Advertisement