Original Post
Hello,
I'm, practicing writing a smart pointer class. My plan was to have each SPtr with a different template parameter contain a static std::list of Allocation structs. The problem is that calling any member function of that std::list results in a crash.
Here are the relevant parts of the code:
template <typename T>
class SPtr
{
...
private:
template <typename T2>
struct Allocation
{
T2 *addr;
int refc;
unsigned int arrlen;
bool dealloc_me;
};
static std::list< SPtr<T>::Allocation<T> > allocs;
...
};
//Static member initialization - Is this done right???
template <typename T>
std::list< SPtr<T>::Allocation<T> > SPtr<T>::allocs;
...
//This is called by some of the constructors:
template <typename T>
void SPtr<T>::Alloc(unsigned int arrlen) throw (std::bad_alloc)
{
DEBUG_PRINT("SPtr<T>::Alloc begin");
... //some irrelevant checks
if (arrlen > 1)
this->ptr = new T[arrlen];
else
this->ptr = new T;
SPtr<T>::Allocation<T> newalc;
newalc.addr = this->ptr;
newalc.refc = 1;
newalc.arrlen = arrlen;
newalc.dealloc_me = true;
DEBUG_PRINT("SPtr<T>::Alloc saving allocation record.");
//CRASH HERE:
SPtr<T>::allocs.push_back(newalc);
this->alc = &allocs.back();
DEBUG_PRINT("SPtr<T>::Alloc end");
}
...
I've also tried calling empty() and insert() in the same place but those crash too.
Compiler: gcc 3.3.2
[edited by - 255 on April 2, 2004 4:13:56 PM]
[edited by - 255 on April 2, 2004 4:41:36 PM]