Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Help debugging boost::variant/boost::flyweight code

16 views
Skip to first unread message

bitrex

unread,
Sep 29, 2017, 3:26:29 PM9/29/17
to
I have the following code using boost::varaint and boost::flyweight.
This wrapper class is used to wrap either an "image bitmap" which is
loaded from a file and key-value flyweighted (so the same data isn't
loaded from storage into memory repeatedly), or a "buffer bitmap" which
is just an internally-generated "scratchpad" that likely won't have much
repetition.

The problem is the segment with the user-defined conversion to
"APIBitmapType*" - I added the temporary instead of returning the result
of applying the visitor directly because the API I'm handing that off
too kept segfaulting when functions taking a particular template type
were called on the conversion. Stepping thru the debugger shows that the
variant returns the address of the appropriate bitmap object just fine,
but on the next line with the return the pointer value immediately
changes to null prior to being returned.

Frustratingly, this code was working OK before until I started making
some tidy-up changes to the header file structure that this class was
originally in; I didn't change the code itself at all. Can't figure out
what I did to break it...

template <typename APIBitmapType>
class BitmapWrapper {
typedef boost::flyweights::flyweight<boost::flyweights::key_value<
const char*, file_bitmap_t<APIBitmapType>>> file_bitmap_flyweight_t;

typedef boost::variant<buffer_bitmap_t<APIBitmapType>,
file_bitmap_t> bitmap_variant_t;

public:
template <typename... Args,
typename = decltype(
buffer_bitmap_t<APIBitmapType>(std::declval<Args>()...))>
explicit BitmapWrapper(Args&&... args)
: _bitmap(std::make_shared<bitmap_variant_t>(

buffer_bitmap_t<APIBitmapType>(std::forward<Args>(args)...))) {}

explicit BitmapWrapper(const char* filename)
: _bitmap(std::make_shared<bitmap_variant_t>(
file_bitmap_flyweight_t(filename))) {}

APIBitmapType* operator&() const
{
return boost::apply_visitor(bitmap_visitor<APIBitmapType>(),
*_bitmap);
}

operator APIBitmapType*() const {
auto test = boost::apply_visitor(bitmap_visitor<APIBitmapType>(),
*_bitmap);
return test;
}

private:
std::shared_ptr<bitmap_variant_t> _bitmap;
};




template <typename APIBitmapType>
class bitmap_visitor : public boost::static_visitor<APIBitmapType*>
{
typedef
boost::flyweights::flyweight<boost::flyweights::key_value<const char*,
file_bitmap_t<APIBitmapType>>>
file_bitmap_flyweight_t;

public:
APIBitmapType* operator()(const file_bitmap_flyweight_t& bitmap) const
{
return bitmap.get().get();
}

APIBitmapType* operator()(const buffer_bitmap_t<APIBitmapType>&
bitmap) const
{
return bitmap.get();
}
};
0 new messages