Skip to main content
GameDev.net gamedev.net
🔒 Locked

C++ static member problem

Started by 255 Apr 2, 2004 at 3:11 PM 4 replies 600+ views
Original Post
255
255
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]
daerid
daerid
In your static definition (outside the class) you're defining a list of pointers, yet inside the class you're declaring a list of objects.

[edited by - daerid on April 2, 2004 4:31:03 PM]
daerid@gmail.com
Bashar
Bashar
When using templates, I believe definition/implementation can''t be split. That''s why you see .hpp files.

One other note, what you''re doing isn''t making sense to me. What''s the point of the template Allocation structure? if addr is simply an address, I''d simply use void* and avoid the mess of templates within templates. Just an opinion
People fear what they don''t understand, hate what they can''t conquer!
255
255
quote:
Original post by daerid
In your static definition (outside the class) you're defining a list of pointers, yet inside the class you're declaring a list of objects.

[edited by - daerid on April 2, 2004 4:31:03 PM]


Fixed now. I forgot to re-copy some changes I made. At first I had made a list of pointers which was really pointless. (first post corrected now)

I'm going to change Allocation to a non-template now and see if that helps. Indeed it is simpler that way. Thanks Bashar.


[edited by - 255 on April 2, 2004 4:46:00 PM]
255
255
Ok here is the new version without the useless T2 for Allocation (all of the code is in one .h file btw. if it matters):


template <typename T>
class SPtr
{
...
private:
struct Allocation
{
T *addr;
int refc;
unsigned int arrlen;
bool dealloc_me;
};

static std::list<SPtr<T>::Allocation> allocs;
...
};

template <typename T>
std::list<typename SPtr<T>::Allocation> 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 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");
}
...


Unfortunately it still crashes.
255
255
Now I tried adding a static test class to SPtr to see if it gets initialized.


template <typename T>
class SPtr
{
...
class test_class
{
public:
test_class() {DEBUG_PRINT("test_class created!");};
~test_class() {DEBUG_PRINT("test_class destroyed!");};
};

static test_class testcls;
...
};

...

template <typename T>
typename SPtr<T>::test_class SPtr<T>::testcls;



That should print "test_class created!" at the very beginning of the program right? Unfortunately that doesn''t happen.

I also tried this with no better results:

template <typename T>
typename SPtr<T>::test_class SPtr<T>::testcls = SPtr<T>::test_class();


What could be the problem?? :-/

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.