Subject: [Boost-bugs] [Boost C++ Libraries] #3658: Remove dependency on static initialization/destruction order
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2009-11-23 07:18:37
#3658: Remove dependency on static initialization/destruction order
------------------------------+---------------------------------------------
Reporter: andysem | Owner: joaquin
Type: Feature Requests | Status: new
Milestone: Boost 1.42.0 | Component: flyweight
Version: Boost 1.41.0 | Severity: Problem
Keywords: |
------------------------------+---------------------------------------------
The static_holder depends on the static initialization/destruction order,
as it is implemented as a function-local static variable. If there are
flyweights in the static or global variables, which are initialized
lazilly, the flyweight value may get destroyed before the flyweights that
refer to it. For example:
{{{
#include <iostream>
#include <string>
#include <boost/shared_ptr.hpp>
#include <boost/flyweight.hpp>
#include <boost/flyweight/key_value.hpp>
struct A
{
int m_n;
A(int n) : m_n(n)
{
std::cout << "A()" << std::endl;
}
~A()
{
std::cout << "~A()" << std::endl;
}
bool operator== (A const& a) const
{
return m_n == a.m_n;
}
friend std::size_t hash_value(A const& a)
{
return a.m_n;
}
};
struct B
{
boost::flyweight< boost::flyweights::key_value< int, A > > m_A;
B() : m_A(10)
{
std::cout << "B()" << std::endl;
}
~B()
{
std::cout << "~B()" << std::endl;
}
};
boost::shared_ptr< B > p;
int main(int, char*[])
{
p.reset(new B());
return 0;
}
}}}
Compiled with MSVC:
{{{
cl -Ox -MD -EHsc -I . -DNDEBUG ./flyweight_test.cpp
}}}
Produces:
{{{
A()
B()
~A()
~B()
}}}
And a crash.
A possible solution would be to destroy flyweight values only when
reference counter from flyweights drops to zero.
-- Ticket URL: <https://svn.boost.org/trac/boost/ticket/3658> Boost C++ Libraries <http://www.boost.org/> Boost provides free peer-reviewed portable C++ source libraries.
This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:01 UTC