D
David Kensche
Hello,
A bus error is caused every time I execute a method. I learned that bus
errors are usually caused if something is going wrong with memory
allocation and dereferencing. But I can't make out the error here.
Can anybody help?
Thanks beforehand,
David Kensche
int pc_record(char* key1, char* key2, record_t record) {
//cout << "pc_record(..): record = " << (int)record << "\n";
Logger *log = new Logger("TermCache.log");
string msg = "pc_record(";
// By the way, this is not very convenient, is there a way to ease
// this setup?
msg += key1;
msg += ", ";
msg += key2;
msg += "): ";
//msg = "pc_record(" + ", " + "): ";
//msg += string(key1) + ", " + string(key2) + "): ";
//msg += key1 + ", " + key2 + "): ";
// The following line causes the bus error.
log->debug(msg + "entered");
int solve = FAIL;
recordMap* cluster = &clusters[key2];
if(cluster->find(key1) == cluster->end()) {
cluster->insert(recordMap::value_type(key1, record));
solve = SUCCEED;
}
delete log;
return solve;
}
And this is the Logger class:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
#define TRACE 0
#define DEBUG 1
#define INFO 2
#define WARN 3
#define FATAL 4
class Logger {
char* logfile;
int level;
public:
Logger(const char* file) {
level = TRACE;
logfile = (char*)malloc(strlen(getenv("HOME")) + 1 + strlen(file));
//free(logfile);
strcpy(logfile, getenv("HOME"));
strcat(logfile, "/");
strcat(logfile, file);
}
void setLevel(int logLevel) {
level = logLevel;
}
void trace(string msg) {
if(level <= TRACE) {
std:fstream out;
out.open(logfile, ios_base:ut | ios_base::app);
out << "TRACE:" << msg << "\n";
out.close();
}
}
void debug(string msg) {
if(level <= DEBUG) {
std:fstream out;
out.open(logfile, ios_base:ut | ios_base::app);
out << "DEBUG:" << msg << "\n";
out.close();
}
}
void info(string msg) {
if(level <= INFO) {
std:fstream out;
out.open(logfile, ios_base:ut | ios_base::app);
out << "INFO:" << msg << "\n";
out.close();
}
}
void warn(string msg) {
if(level <= WARN) {
std:fstream out;
out.open(logfile, ios_base:ut | ios_base::app);
out << "WARN:" << msg << "\n";
out.close();
}
}
void fatal(string msg) {
if(level <= FATAL) {
std:fstream out;
out.open(logfile, ios_base:ut | ios_base::app);
out << "FATAL:" << msg << "\n";
out.close();
}
}
private:
void operator=(const Logger&);
void operator&();
void operator,(const Logger&);
};
A bus error is caused every time I execute a method. I learned that bus
errors are usually caused if something is going wrong with memory
allocation and dereferencing. But I can't make out the error here.
Can anybody help?
Thanks beforehand,
David Kensche
int pc_record(char* key1, char* key2, record_t record) {
//cout << "pc_record(..): record = " << (int)record << "\n";
Logger *log = new Logger("TermCache.log");
string msg = "pc_record(";
// By the way, this is not very convenient, is there a way to ease
// this setup?
msg += key1;
msg += ", ";
msg += key2;
msg += "): ";
//msg = "pc_record(" + ", " + "): ";
//msg += string(key1) + ", " + string(key2) + "): ";
//msg += key1 + ", " + key2 + "): ";
// The following line causes the bus error.
log->debug(msg + "entered");
int solve = FAIL;
recordMap* cluster = &clusters[key2];
if(cluster->find(key1) == cluster->end()) {
cluster->insert(recordMap::value_type(key1, record));
solve = SUCCEED;
}
delete log;
return solve;
}
And this is the Logger class:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
#define TRACE 0
#define DEBUG 1
#define INFO 2
#define WARN 3
#define FATAL 4
class Logger {
char* logfile;
int level;
public:
Logger(const char* file) {
level = TRACE;
logfile = (char*)malloc(strlen(getenv("HOME")) + 1 + strlen(file));
//free(logfile);
strcpy(logfile, getenv("HOME"));
strcat(logfile, "/");
strcat(logfile, file);
}
void setLevel(int logLevel) {
level = logLevel;
}
void trace(string msg) {
if(level <= TRACE) {
std:fstream out;
out.open(logfile, ios_base:ut | ios_base::app);
out << "TRACE:" << msg << "\n";
out.close();
}
}
void debug(string msg) {
if(level <= DEBUG) {
std:fstream out;
out.open(logfile, ios_base:ut | ios_base::app);
out << "DEBUG:" << msg << "\n";
out.close();
}
}
void info(string msg) {
if(level <= INFO) {
std:fstream out;
out.open(logfile, ios_base:ut | ios_base::app);
out << "INFO:" << msg << "\n";
out.close();
}
}
void warn(string msg) {
if(level <= WARN) {
std:fstream out;
out.open(logfile, ios_base:ut | ios_base::app);
out << "WARN:" << msg << "\n";
out.close();
}
}
void fatal(string msg) {
if(level <= FATAL) {
std:fstream out;
out.open(logfile, ios_base:ut | ios_base::app);
out << "FATAL:" << msg << "\n";
out.close();
}
}
private:
void operator=(const Logger&);
void operator&();
void operator,(const Logger&);
};