Boost logo

Boost Users :

From: Jeremy Hill (jeremyhill_at_[hidden])
Date: 2005-02-21 04:26:59


Greetings,

I have only recently begun exploring boost, and I must say that I have
found it
very interesting. In particular I've been focusing on boost::shared_ptr.

However, I'm not quite sure how to 'deallocate' shared_ptrs that are in a
Container, such as a vector. I'd like to issue some pseudocode to help put
things in perspective:

class Descriptor
{
    public:
        bool valid;
        Descriptor() : valid(true) {}
}

struct InvalidDesc
{
    bool operator()(Desc_ptr &d) { return (*d).valid; }
};

class Desc_broker
{
    private:
        vector< boost::shared_ptr< Descriptor > > desc_list;
    public:
        //These functions are, in this order, called elsewhere:
        void fun_1()
        {
            <conditionally invalidate Descriptors>
        };
        void fun_2()
        {
            <iterate through desc_list and do stuff>
        };
        void cleanup()
        {
           desc_list.erase( remove_if(desc_list.begin(), desc_list.end(),
               InvalidDesc()), desc_list.end() );
        }
};

There is the class 'Descriptor' that is contained within a shared_ptr,
which is
wrapped in the 'Desc_broker' class. Once a Descriptor is invalidated,
such as
in fun_1() above, it is no longer needed. I'll need to get rid of invalid
Descriptors before the vector is iterated through in fun_2(). However,
since
the shared_ptr for the invalid Descriptor will not go out of scope, to the
best of my knowledge I'll need to help it along by using something along the
lines of 'cleanup', which will remove all desc_ptrs whose Descriptors are
invalid (via std::remove_if and the InvalidDesc predicate).

However, this feels wrong to me because it seems like it is against what
shared_ptrs are trying to accomplish. And, in fact, Boost agrees--I receive
this on cleanup():

assertion "px != 0" failed: file
"/usr/local/include/boost/shared_ptr.hpp", line 253
Aborted (core dumped)

What is the correct way to remove the invalid Descriptors?

Also, I apologize if this is considered the unacceptable topic "Requests for
help interpreting the C++ standard." If that is the case, please tell me so
that I may not offend in future posts.

Thank you,
Jeremy Hill


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