Segmentation fault / using std::map
Hello. I think I have pretty much isolated my problem though I do not understand it. I am trying to do a really simple class to get information from a configuration file and store those settings. I guess it is some kind of basic finite state machine. I am currently trying to make it work with my debug class but for some reason the condition that checks if "logfile.name" is set always returns true, causing a failed attempt to open file and for some reason causes a segmentation fault. All code in main() is commented and it is the only piece of code that actually runs at this moment. I don't think the file opening class is causing any trouble here as I have opened files with it before without trouble so I will not include its code here.
What I'm pretty sure of is that my code inside IsStateSet() is wrong, but I do not know really how to check for empty strings using maps. I had return states[state] != "" before but it caused the very same issue.
Code:
//
// system.cpp
//
#include "system.h"
#include "iomanagement.h"
//*****************************
// Error logging implementation
//*****************************
#ifdef DEBUG_MODE
C_DBG_LOGGER::C_DBG_LOGGER() {
if(MASTER_CONFIG.IsStateSet("logfile.name")) {
output.OpenFile(MASTER_CONFIG.ReadState("logfile.name"));
} else {
output.OpenFile("dbg_log.txt");
}
}
void C_DBG_LOGGER::Write(const char* msg) {
if(output.IsFileOpen()) {
output.WriteToFile(msg);
}
}
C_DBG_LOGGER DBG_LOGGER;
#endif // DEBUG_MODE
//************************************
// Finite state machine implementation
//************************************
FSMachine::FSMachine() {
}
bool FSMachine::IsStateSet(std::string state) {
return states[state].size() > 5; // (minimum is x.xxx, therefore 5)
}
void FSMachine::SetState(std::string state, std::string& value) {
states[state] = value;
}
std::string FSMachine::ReadState(std::string state) {
return states[state];
}
FSMachine MASTER_CONFIG;
Code:
//
// system.h
//
#ifndef SYSTEM_H
#define SYSTEM_H
#define DEBUG_MODE
#include <map>
#include <string>
//*********************************************
// Setup the general macro that will be used to
// write errors to log file. Only define it if
// debugging is desired.
//*********************************************
#ifdef DEBUG_MODE
#include "iomanagement.h"
class C_DBG_LOGGER {
private:
Writer output;
public:
C_DBG_LOGGER();
void Write(const char*);
};
extern C_DBG_LOGGER DBG_LOGGER;
#define DIE(a) DBG_LOGGER.Write(a)
#else
#define DIE(a) //..do nothing
#endif // DEBUG_MODE
//***************************
// Basic finite state machine
//***************************
class FSMachine {
private:
std::map<std::string, std::string> states;
public:
FSMachine();
bool IsStateSet(std::string);
void SetState(std::string, std::string& value);
std::string ReadState(std::string);
};
extern FSMachine MASTER_CONFIG;
#endif // SYSTEM_H
Any insight is very much welcome.
Note: Of course I tried using the debugger but for some reason it is not working (error on start). I am looking into that right now.
Thanks.