Skip to main content
gamedev.net
🔒 Locked 🎮 Unity

Pointer pointer troubles

Started by eXt_ Jul 12, 2006 at 4:08 PM 3 replies 700+ views
Original Post
eXt_
eXt_
Can I do a function that takes a pointer to a pointer to a baseclass. I'm doing retaincounting in the baseclass and when calling a release method I check if the count reached zero and then deletes the object. However, sometimes double-releases sneaks into the code and everything crashes. This error can take hours to find. So what I'm trying to do is a function that takes a pointer to the pointer and will first release the object and then reset the pointer to NULL. This works only if I have a pointer to the baseclass, not of it's childrens. I get errors like this when compiling:

error: invalid conversion from "Vector3**" to "Object**"
error:   initializing argument 1 of "void release(Object**)"
I thought that void** would solve this but the same error occures. Any ideas?
SiCrane
SiCrane
You can use a template function. ex:
template <typename T>void delete_and_null(T * & ptr) {  delete ptr;  ptr = 0;}
eXt_
eXt_
I keep getting "undefined reference to `void release(Vector3*&)'". This is the code I'm using:

Object.h:
class Object {	public:		template <typename T> friend void release(T * & ptr);	private:		uint8_t _retainCount;};template <typename T> void release(T * & ptr);

Object.cpp:
template <typename T>void release(T * & ptr) {	ptr->_retainCount--;	if ( ptr->_retainCount == 0 ){		delete ptr;		ptr = NULL;	}}

And this is what I do when I get the linker errors:
Vector3 v = new Vector3;release(v);
SiCrane
SiCrane
Unless your compiler supports the export keyword, and I'm willing to bet that yours doesn't, then you can't put the definition of a template in a separate source file without explicit instantiation for specific types. Without explicit instantiation, the complete definition of the template needs to be available at point of instantiation, which means, in effect, that the definition needs to go into the header. (Or an inline file of some sort, etc.)
eXt_
eXt_
Ahh, yes that helped. Thank you! (I'm using gcc-4.1.1 btw)

Topic Locked

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

Sign in to reply to this topic.