Segmentation Fault

SEE MOST RECENT POST OF MINE FOR UP TO DATE ISSUES!

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
#include <string>
#include <sstream>
#include <fstream>
#include <iostream>

#include "itemManager.hpp"

void item::loadItem(int itemID){

	std::string filename;
	
	std::string s_itemID;
	std::ostringstream converter;
	converter << itemID;
	s_itemID = converter.str();
		
	filename.assign(s_itemID);
	filename.append(".txt");
	
	std::ifstream file;
	file.open(filename.c_str());
	
	std::getline(file, this->name);
	
	int i_properties = 13;
	int i_buffer[i_properties];
	
	for (int x = 0; x <= i_properties; x++){
	
		file >> i_buffer[x];
	
	}
	
	this->canEquip = i_buffer[0];
	this->equipSlot = i_buffer[1];
	this->min_meleeDMG = i_buffer[2];
	this->max_meleeDMG = i_buffer[3];
	this->min_rangedDMG = i_buffer[4];
	this->max_rangedDMG = i_buffer[5];
	this->min_magicDMG = i_buffer[6];
	this->max_magicDMG = i_buffer[7];
	this->armorRating = i_buffer[8];
	this->specialUse = i_buffer[9];
	this->addHealth = i_buffer[10];
	this->addMana = i_buffer[11];
	this->priceSell = i_buffer[12];
	this->priceBuy = i_buffer[13];


}


Specifically, std::getline(file, this->name); is causing the segmentation fault. I do not see why. It was working until I began adding more code onto the project in other places unrelated to this portion. The objective of this code is to load information off a .txt file outlining the information to an in-game item.

Could anyone help me figure out why this is giving me a segmentation fault?

Thank you very much!

Nathaniel
Last edited on
As far as I can see, the only thing that should cause a segmentation fault is line 28 and line 47 (accessing past the end of the array). Also, really, you should declare line 25 constexpr, VLA's (variable length arrays) aren't really legal until C++14.

Could we see your class? You might be doing something with name that isn't right (name is a std::string I presume).
Last edited on
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
#ifndef ITEMMANAGER_H
#define ITEMMANAGER_H

#include <string>

class item{

public:
//basic properties of the item
	std::string name;
	bool canEquip;
	bool specialUse;
	int equipSlot; //1 = head; 2 = neck; 3 = chest; 4 = legs; 5 = hands; 6 = mainhand; 7 = offhand; 8 = feet;
	int priceSell; //0 = cannot be sold
	int priceBuy;
	
//stat properties of item
	int min_meleeDMG;
	int max_meleeDMG;
	int min_rangedDMG;
	int max_rangedDMG;
	int min_magicDMG;
	int max_magicDMG;
	int armorRating;
	
//special properties of item
	int addHealth;
	int addMana;
	
	void loadItem(int itemID);

};

#endif 


There is the class. I have noticed that variable length arrays have been giving me some trouble. I am still a novice, I am using this project as a learning experience. So far It has come along with minimal issue though.

Potentially, the error could be located here too, although I don't see the issue with this either, but when I comment out the portion where this function is called, the program returns error-free:

1
2
3
4
5
6
7
8
9
10
void inventory::init(){

	for (int x = 0; x < 28; x++){
		
		slot[x].itemID = 0;
		slot[x].itemData.loadItem(0);
	
	}

}


0.txt:
Empty
0
0
0
0
0
0
0
0
0
0
0
0
0
0


The code worked fine until I added a new class to load NPCs into the game in a similar way:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef NPCMAN_H
#define NPCMAN_H

#include <string>

class npcManager{

public:
	std::string name;
	bool aggressive;
	int health;
	int mana;
	int maxHealth;
	int maxMana;
	std::string dialogue;
	
	void loadData(int npcID);

};

#endif 


but even with the removal of this code, the program continues to give me that segmentation fault error.
Last edited on
I have re-written the system using RapidXML; a completely new file type, and a new system, yet I am still getting the same segmentation fault that I received with the old system.

The code is as follows:

fileManager.hpp
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <string>
#include <fstream>
#include <sstream>

#include <rapidxml.hpp>
#include <rapidxml_print.hpp>
#include <rapidxml_utils.hpp>

#include "fileManager.hpp"

void filesystem::exportItem(item itemData, int itemID){
	
	std::ostringstream convert;
	
	//convert the itemID into a string as a filename, for example, 1.item
	std::string filename;
	convert << itemID;
	filename = convert.str();
	filename.append(".item");
	
	//convert each the data from the item into strings
	std::string equipmentSlot;
	std::string armorRating;
	std::string minDmg;
	std::string maxDmg;
	
	convert << itemData.equipment_slot;
	equipmentSlot = convert.str();
	convert << itemData.armor_rating;
	armorRating = convert.str();
	convert << itemData.min_damage;
	minDmg = convert.str();
	convert << itemData.max_damage;
	maxDmg = convert.str();
	
	
	//open the XML document to write to
	rapidxml::xml_document<> doc;
	
	//write the basic information to the document
	rapidxml::xml_node<>* decl = doc.allocate_node(rapidxml::node_declaration);
	decl->append_attribute(doc.allocate_attribute("version", "1.0"));
	decl->append_attribute(doc.allocate_attribute("encoding", "utf-8"));
	doc.append_node(decl);
	
	//write the item's attributes to the rootnode
	rapidxml::xml_node<>* root = doc.allocate_node(rapidxml::node_element, "rootnode");
	root->append_attribute(doc.allocate_attribute("itemName", itemData.name.c_str()));
	root->append_attribute(doc.allocate_attribute("itemDescription", itemData.description.c_str()));
	root->append_attribute(doc.allocate_attribute("icon", itemData.icon.c_str()));
	root->append_attribute(doc.allocate_attribute("equipmentSlot", equipmentSlot.c_str()));
	root->append_attribute(doc.allocate_attribute("armorRating", armorRating.c_str()));
	root->append_attribute(doc.allocate_attribute("minDMG", minDmg.c_str()));
	root->append_attribute(doc.allocate_attribute("maxDMG", maxDmg.c_str()));	
	
	rapidxml::xml_node<>* child = doc.allocate_node(rapidxml::node_element, "childnode");
	root->append_node(child);
	
	//write the information to the file, for example, 1.item
	std::string xml_as_string;
	rapidxml::print(std::back_inserter(xml_as_string), doc);
	
	std::ofstream file(filename.c_str());
	file << doc;
	file.close();
	doc.clear();
	

}

void filesystem::importItem(item itemData, int itemID){
	
	std::ostringstream convert;
	
	//convert the itemID into a string as a filename, for example, 1.item
	std::string filename;
	convert << itemID;
	filename = convert.str();
	filename.append(".item");
	
	//open the XML document
	rapidxml::xml_document<> doc;
	rapidxml::file<> file(filename.c_str());
	doc.parse<0> (file.data());
	
	std::string name;
	std::string description;
	std::string icon;
	std::string equipmentSlot;
	std::string armorRating;
	std::string minDMG;
	std::string maxDMG;
	
	//open the rootnode where all the information is saved
	rapidxml::xml_node<> *n = doc.first_node("itemAttributes")->first_node();
	
	//read each individual attribute from the XML document
	convert << n->first_attribute("itemName")->value();
	name = convert.str();
	itemData.name = name;
	
	convert << n->next_sibling()->value();
	description = convert.str();
	itemData.description = description;
	
	convert << n->next_sibling()->value();
	icon = convert.str();
	itemData.icon = icon;
	
	convert << n->next_sibling()->value();
	equipmentSlot = convert.str();
	int v_equipmentSlot = atoi(equipmentSlot.c_str());
	itemData.equipment_slot = v_equipmentSlot;

	convert << n->next_sibling()->value();
	armorRating = convert.str();
	int v_armorRating = atoi(armorRating.c_str());
	itemData.armor_rating = v_armorRating;

	
	convert << n->next_sibling()->value();
	minDMG = convert.str();
	int v_minDMG = atoi(minDMG.c_str());
	itemData.min_damage = v_minDMG;
	
	convert << n->last_attribute()->value();
	maxDMG = convert.str();
	int v_maxDMG = atoi(maxDMG.c_str());
	itemData.max_damage = v_maxDMG;
	

}


I have noticed that the .item file itself when opened in notepad looks as follows:

<?xml version="1.0" encoding="utf-8"?>


Potentially, I would think that the export function is not exporting it correctly, preventing the import function from reading it.

I am just picking up the use of RapidXML.
Last edited on
Topic archived. No new replies allowed.