Second instance of KDTree::insert fails when destructor is called

Ingo Jenni ijenni at ee.ethz.ch
Mon Jul 27 15:41:07 UTC 2009


Hi everybody!

I want to insert structures containing a dynamically allocated element  
into the tree:

struct Keypoint
{
	unsigned int* descriptor;
	size_t descLen;

	Keypoint(unsigned int* desc_pt, size_t _descLen)
	{
		descLen = _descLen;
		descriptor = new unsigned int[descLen];

		for(int i = 0; i != descLen; i++)
		{
			descriptor[i] = desc_pt[i];
		}
	}

	~Keypoint()
	{
		delete descriptor;
	}

	unsigned int operator[](size_t const N) const {return descriptor[N];}
};

// Vector access function
unsigned int descAcc(Keypoint k, size_t n)
{
	return k[n];
}

unsigned int* desc1 = new unsigned int[descLen];
unsigned int* desc2 = new unsigned int[descLen];

Keypoint k1(desc1, descLen);
Keypoint k2(desc2, descLen);

typedef KDTree::KDTree<descLen, Keypoint,  
std::pointer_to_binary_function<Keypoint, size_t, unsigned int> > Tree;


Tree tree(std::ptr_fun(descAcc));

tree.insert(k1);
tree.insert(k2);

Now the first insert works fine, but the second one fails with a crash:

*** glibc detected *** ./a.out: double free or corruption (fasttop):  
0x08ff6038 **

This error does not happen when I comment out the destructor of  
Keypoint, but that would be the dirty solution by my count :-)

Does anyone have an idea what's going on here?

Best regards
Ingo





More information about the libkdtree-devel mailing list