Re: VS2008 destroys static objects before global (non-static) objects?
Boris wrote:
On Wed, 22 Oct 2008 15:58:50 +0200, Ben Voigt [C++ MVP]
<rbv@nospam.nospam> wrote:
My problem explained in pseudocode is this:
----------
template <typename T>
class memory_manager
{
public:
void *operator new(std::size_t) { return mem_p_.malloc(); }
void operator delete(void *p) { p_.free(p); }
private:
static pool p_;
};
class small_object : public memory_manager<small_object>
{
};
Well, you're missing the definition of
memory_manager<small_object>::p_
I left out a couple of more things - that's why I called it
pseudocode. :)
It has global lifetime (although private visibility). So you have
some difficulty controlling the construction and destruction order,
yes.
Are there any VC++ directives or compiler/linker switches to make
sure all static objects are destroyed last? I guess the pseudocode
It's not a static object. It's a global object stored in a static variable.
See, the line in the header file which creates the variable doesn't create
the object, you need a separate line at global scope in an implementation
file to create the object.
above is pretty clear that p_ must be destroyed after all instances
of small_object are destroyed. A directive or switch would be a
short-term solution without me having to rewrite the entire memory
management of the software now.
You could take a dependency on a DLL where the object actually resides,
because all global destructors in the program are run before any statically
linked DLLs can be unloaded (causing their global destructors to run).
Note that I'm not overly fond of this solution because it means
dllexport/dllimport on something that isn't C-compatible.