Aggravating Segfault

I've been staring at this code for about an hour, and I can't find the bug. It should be something simple, but I don't see it. This is not my first time working with pointers and dynamic memory, but I am not very skilled at it.

I am following a tutorial, but it is written in C, so I decided to try and convert it to c++(and OOP).The basic goal of this is to create a 2d skeleton using a hierarchy(children and parent). I should be able to add a bone, so that I can make a human skeleton. I am testing the shape with a starfish, which is a lot simpler. This is the relevant code:

The bone class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
class Bone{
	private:

	public:
	Bone();
	~Bone();
	
	Bone *AddChild(Bone *root, float x, float y, float angle, float length, 
			uint8 flags, std::string name);
			
	float x, y, angle, length;
	uint8 flags;
	int childCount;
	std::string name;
	Bone *child[MAX_CHILD_COUNT], *parent;
};

Bone::Bone(){
	x = y = angle = length = 0;
	childCount = 0;
	for (int i = 0; i < MAX_CHILD_COUNT; i++)
		this->child[i] = NULL;
}

Bone::~Bone(){
	if(this != NULL)
		delete this;
}

Bone *Bone::AddChild(Bone *root, float x, float y, float angle, float length, 
			uint8 flags, std::string name = NULL){ 
	Bone *tmp;

	
	if(!root){
		if(!(root = new Bone))
			return NULL;
		root->parent = NULL;
	}else if(root->childCount < MAX_CHILD_COUNT){//if there is space left
		if(!(tmp = new Bone))
			return NULL;
		tmp->parent = root;
		
		root->child[root->childCount++] = tmp;
		root = tmp;
	}else
		return NULL;
		
	root->x = x;
	root->y = y;
	root->angle = angle;
	root->length = length;
	root->flags = flags;
	root->childCount = 0;
	
	if(!name.empty())
		root->name = name;
	else
		root->name = "Bone";
	return root;
}


In main():
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
	Bone *root, *tmp, *tmp2;
	
	if(!(root->AddChild(NULL, 100, 100, 0, 0, 0, "NULLBone"))){
		fprintf(stderr, "Error! Can't create root!\n");
		return EXIT_FAILURE;
	}
 	//this draws the starfish
	root->AddChild(root, 100, 100, M_PI_2, 10, BONE_ABSOLUTE, "Head");
 	tmp->AddChild(root, 0, 0, -M_PI_2, 30, 0, "Back");
  	tmp2->AddChild(tmp, 0, 0, -M_PI_4, 30, 0, "LLeg");
 	root->AddChild(tmp2, 0, 0, 0, 30, 0, "LLeg2");
 	tmp2->AddChild(tmp, 0, 0, -2 * M_PI_4, 30, 0, "RLeg");
  	root->AddChild(tmp2, 0, 0, 0, 30, 0, "RLeg2");
 	tmp->AddChild(root, 0, 0, 0, 20, 0, "LArm");
 	root->AddChild(tmp, 0, 0, 0, 20, 0, "LArm2");
 	tmp->AddChild(root, 0, 0, M_PI, 20, 0, "RArm");
 	root->AddChild(tmp, 0, 0, M_PI, 20, 0, "RArm2");
Last edited on
Where are getting the seg fault?
This is not related to your problem, but a bone doesn't contain bones

Did you locate where the segfault occurs?
1
2
3
4
Bone::~Bone(){
	if(this != NULL)
		delete this; //stack overflow
}
If you are in the destructor, that means that your "this" is being deleted already
@bartoli
The bone doesn't contain another bone, but points to another on, like how a skeleton is linked together.

@ne555
I am still new to this, how does this cause a stack overflow? Is this to big for delete?

Oh I see thank you :)

Last edited on
Now it gives me this :( (in gdb)

#0 0x00380116 in malloc () from /lib/tls/i686/cmov/libc.so.6
#1 0x00292c07 in operator new(unsigned int) () from /usr/lib/libstdc++.so.6
#2 0x0026cd06 in std::string::_Rep::_S_create(unsigned int, unsigned int, std::allocator<char> const&) () from /usr/lib/libstdc++.so.6
#3 0x0026db11 in ?? () from /usr/lib/libstdc++.so.6
#4 0x0026dcf6 in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&) ()
from /usr/lib/libstdc++.so.6
#5 0x08049536 in main (argc=1, argv=0xbffff484) at 2dskel.cpp:143


Is it related? line 143 is this:
tmp->AddChild(root, 0, 0, -M_PI_2, 30, 0, "Back");
You have not assigned tmp a value, so it points to un undefiined address and will cause a segfault when you dereference it
Much Thanks :D
Shouldn't there be a variable there since I used new? What should I do?
The variable tmp in AddChild is assigned the address of a new Bone. But the tmp in main is not assigned any value
Thank you ^_^
Topic archived. No new replies allowed.