Hi there,

I'm having trouble with my program, which crashes when accessing values through a pointer. I've created a sample program that demonstrates my problem.

Little synopsis:
A Character has 2 attributes, a Bone, and a vector of pointers to Bones.

I have a parser file that has functions to create a Character.

Another file calls the functions in the parser file

Character.h

#ifndef GUARD_Character_h
#define GUARD_Character_h

#include <iostream>
#include <vector>
#include <string>
#include "Bone.h"
#include "Character.h"

class Character {
public:
	// ATTRIBUTES
	std::vector<Bone> bones;
	std::vector<Bone*> bone_pointers;

	// CONSTRUCTORS
	Character()	{};
	
	// ACCESSORS
	std::vector<Bone>& get_bones()
	{
		return bones;	
	};

	void add_ptrs(std::vector<Bone*> b)
	{
		bone_pointers = b;	
	};

	void add_bones(std::vector<Bone> b)
	{
		bones = b;	
	};
};
#endif

Bone.h

#ifndef GUARD_Bone_h
#define GUARD_Bone_h

#include <string>
#include <vector>
#include <iostream>

class Bone {
public:
	// ATTRIBUTES
	std::string bone_id; 
	std::vector<Bone*> children;

	// CONSTRUCTORS
	Bone(std::string s)
	{
		bone_id = s;	
	};
	
	// ACCESSORS
	std::string& get_id()
	{
		return bone_id;	
	};
	std::vector<Bone*>& get_children()
	{
		return children;
	};

	// MANIPULATORS
	void add_child(Bone* child)
	{
		children.push_back(child);
		
	};
};
#endif

Parser functions

Character& load_x_file()
{
	static Character ch = load_character();
	cout << "##COUT NO. 1## In 'load_x_file()', Character::bone pointer is: " << (*ch.bone_pointers[0]).get_id() << endl;
	return ch;
}

// Constructs a Character with bones and vector of pointers
Character& load_character()
{
	static Character ret;
	vector<Bone> bones = read_skeleton();
	vector<Bone*> bone_ptrs = read_skinning_info(bones);
	ret.add_bones(bones);
	ret.add_ptrs(bone_ptrs);

	cout << "##COUT NO. 2## In 'load_character()', Character::bone pointer is: " << (*ret.bone_pointers[0]).get_id() << endl;
	return ret;

}

// Creates a vector of 2 bones, Bone one is the parent of bone two
std::vector<Bone>& read_skeleton()
{
	static vector<Bone> bones;
	Bone b1 = Bone("one");
	Bone b2 = Bone("two");
	bones.push_back(b1);
	bones.push_back(b2);
	Bone* ptr = &bones[1];
	bones[0].add_child(ptr);
	return bones;
}

vector<Bone*>& read_skinning_info(vector<Bone>& bones)
{
	Bone* b = &bones[0];
	vector<Bone*> bones_ptr;
	bones_ptr.push_back(b);

	return bones_ptr;
}

Here is the main function of another file calling the parser functions:

int main()
{
	Character test = load_x_file();
	std::cout << "##COUT NO. 3## The pointers in main() point to: " << (*test.bone_pointers[0]).get_id() << std::endl;
}

Running the program causes a crash at ##COUT NO. 2##. Running the debugger doesn't pick up any problems (Visual C++ 2008), looking at the values at each point in the program, all pointers look to be pointing at valid values throughout the program.

Can someone please tell me what's going on here? :icon_confused:

Thanks in advance,

Tom

Recommended Answers

All 3 Replies

Try a little redesign of the program by removing all those static objects and pass them by reference into the functions, such as void load_character(Character& ret) Your program will use less memory that way too.

I finally got it to compile with vc++ 2008 Express. Get this warning on function read_skinning_info() which you can not just ignore

warning C4172: returning address of local variable or temporary

Apologies Dragon, the read_skinning_info function in the post was out of sync with the one in my compiler.

Thanks for the redesign tip, always nice to find way to improve the overall structure.

The redesign fixed my problem, which I'm am very grateful for :icon_smile:

However, before I close the thread as solved, do you have any idea why the code as posted (by following my previous method of staticing everything lol, and changing "vector<Bone*> bones_ptr" to be static) wasn't working? Would be interested to know.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.