Boost logo

Boost Users :

Subject: [Boost-users] [Boost::any] Memory management using dynamic libraries
From: Eugenio Bargiacchi (svalorzen_at_[hidden])
Date: 2013-12-25 10:26:25

I'm building a framework which needs to allow communication between
shared libraries which are loaded at runtime. Each shared library
provides its own implementation of an abstract interface contained in
the framework (called Module), and all Modules are called sequentially
multiple times doing the work they need to do.

Since modules need to communicate with each other with custom data
types, I though of using an std::unordered_map<std::string,boost::any>
kept by the framework and shared with the Modules. The map is filled
and read by the modules when their code is called through the Module
interface which the framework knows (virtual functions).

However after a bug I realized that each boost::any value is allocated
by the modules, which is logical, since the framework itself has no
knowledge of the types that will be stored in the map, and thus the
code to allocate them must reside in the libraries themselves. I was
wondering whether this could be a problem both during overwriting of a
map value (since boost::any has to destroy its old contents) and
during program shutdown. This could maybe be because modules use their
own allocators.

The problem that I had, for example, resulted in a segfault if I tried
to unload the modules from memory before the destruction of the map
(which is owned by the framework), during boost::any destruction. I
assume this happened because the contents of boost::any were created
in memory that was then unloaded during the unloading of the shared
libraries, resulting in the segfault. And this yet does not take into
account custom allocators!

So I'm wondering what the best approach here would be. If I would need
to keep track of the current "owner" of each single boost::any value
allocated, and use it to manually destroy instances when required, or
if there is a better way, maybe using some particular boost::any

Many thanks in advance!

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at