tags:

views:

49

answers:

1

Hi all,

I have a C++ program with a reference-counting smart pointer class. This class works by mapping pointers to reference counts in a static map:

map<ValueIntern*,unsigned int>& ValueRetainMapGetter(){
    static map<ValueIntern*,unsigned int> m;
    return m;
}

The issue that I've been having is that some static variables which I have are being deallocated after the reference map has been deallocated.

My question is: how can I control the order in which the static variables are deallocated so that the map is deallocated after all of the references.

+2  A: 

I'd recommend using boost::shared_ptr (or std::tr1::shared_ptr if it's in your tool chain) instead of rolling your own.

Cogwheel - Matthew Orlando
There are even many flavors of boost:: smart pointers to choose from: http://www.boost.org/doc/libs/1_43_0/libs/smart_ptr/smart_ptr.htm
jdmichal
Wow, I feel really smart...but good advice. The reason I was using a map was because originally, when I first wrote the class, it was a template class, desigened for use by a myriad of other classes.
Maz
@Maz: The thing isn't a _class_, it's a _template_. A template from which one can instantiate classes. That's why it's called a __class template__. If it were called "template class", it would have to be a class from which templates...um...I don't know. See. That's why it's called a "class template". (There are, BTW, also "function templates". These are _templates_ from which functions are instantiated. They are not, I repeat: not, "template functions", because they are not functions from which...um...here we go again. So that's why they are named __function templates__.)
sbi