Sign in to follow this  
Tradone

=Error accessing memory address 0x0: Bad address.

Recommended Posts

I am so sick and tired of these errors. What the hell is causing them? I know that they're pointer related.
//main.cpp
Data *tempData=new Data;
tempData->Set( "system/db/" + controlParameter->Get("db") + "/config.cgi" );

//Data.h
#if !defined(DATA)
#define DATA

#include <fstream>
#include <string>
#include <iostream>
#include <map>

#include "Singleton.h"
#include "Algorithms.h"
#include "Config.h"

#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/map.hpp>
//#include "CaseInsensitiveMapOrder.h"

#pragma comment(lib, "libboost_serialization-vc71-sgd.lib")

class Data : public Config{
	public:
		Data();
		void Set(std::string filePath);
		void Set(std::string key, std::string value);
		void Save();
	//end public:

	private:
		std::string filePath;
};
#endif


//Config.h
#if !defined(CONFIG)
#define CONFIG

#include <string>
#include <iostream>
#include <map>
#include <boost/algorithm/string.hpp>

#include "Singleton.h"
#include "Algorithms.h"

class Config{
	public:
		Config();
		void Set(std::string path);
		void Set(std::string key, std::string value);
		void Save();

		std::map <std::string, std::string> Get();
		std::map <std::string, std::string>* pGet();
		std::string Get(std::string& key);
		std::string Get(const std::string& key);
	//end public:

	protected:
		std::string filePath;
		std::map <std::string, std::string> config;
	//end private:
};
#endif

//Data.cpp
void Data::Set( std::string path ){
/******ERROR********/
	filePath=path; //ERROR
/******ERROR********/
	{
        // create and open an archive for input
        std::ifstream ifs(filePath.c_str(), std::ios::binary);
        boost::archive::text_iarchive ia(ifs);
        // read class state from archive
        ia >> config;
        // archive and stream closed when destructors are called
    }

	for( std::map<std::string,std::string>::iterator it = config.begin(); it != config.end(); it++) {
		config[(*it).first] = algorithms::Instance().XOR_String( (*it).second,'a' );
	}

	if( config["count"] == "" || !boost::algorithm::all(config["count"], boost::algorithm::is_digit()) )
		Set("count","0");
	if ( config["downcount"] == "" || !boost::algorithm::all(config["downcount"], boost::algorithm::is_digit()) )
		Set("downcount","0");

}

#24 0x08062c67 in Data::Set (this=0x80b0c60, path=Error accessing memory address 0x0: Bad address. ) at Data.cpp:18 #25 0x08081c95 in Control::DisplayCont (this=0x80aa080) at Control.cpp:76 #26 0x0804d722 in main (argc=4, argv=0xbfbfec18) at new_shenu.cpp:46

Share this post


Link to post
Share on other sites
controlParameter->Get("db") returns the string "main" and yes I have tested that already.

There seems to be NO problem with the source file that I've provided. seriously LOL
and I know this problem will sooner or later be solved, because I always solve them by either dismantling my whole project ( total project size = 120k w/ 34 files and 15 classes ) or by debugging using std::cout ( which sometimes I think the cout method takes much longer )

SERIOUSLY, I CAN'T DO THIS ANYMORE.
I spend the whole day trying to find one bug.
Somebody NEEDS to recommend me some better debugging methods and also how to avoid those "Error accessing memory address 0x0: Bad address errors." b/c I get these errors every week.

PLEASE SOMEBODY HELP ME!! SOS!!

Share this post


Link to post
Share on other sites
As pointed out, this is mostly likely caused by ControlParameter being NULL at the time you called Get(). That would usualy be the case if an initialization of the class failed during runtime. You should check to make sure the class was actualy create after its creation, C++ makes this easy as you can just check to make sure that it was true, something like the following.

ControlParameter control_parameter = System.GetCP();
if(!control_parameter)
{
throw SomeException;
}

Share this post


Link to post
Share on other sites
I have no idea what you mean by what you just said, but the other possibility is that the tempdata constructor failed, and you can test for that as I pointed out. But I have never encountered that in practice either.

Share this post


Link to post
Share on other sites
This is the whole Data.h source file.
the Data Class is a subclass of the Config class,
and filePath is a protected member variable of the Config class.
could
Data::Data(){
filePath="";
}
caused the problem?


#include "Data.h"

typedef Singleton<Algorithms> algorithms;

Data::Data(){
filePath="";
}

void Data::Set( std::string key, std::string value ){
config[key]=value;
}

void Data::Set( std::string path ){
filePath=path;
{
// create and open an archive for input
std::ifstream ifs(filePath.c_str(), std::ios::binary);
boost::archive::text_iarchive ia(ifs);
// read class state from archive
ia >> config;
// archive and stream closed when destructors are called
}

for( std::map<std::string,std::string>::iterator it = config.begin(); it != config.end(); it++) {
config[(*it).first] = algorithms::Instance().XOR_String( (*it).second,'a' );
}

if( config["count"] == "" || !boost::algorithm::all(config["count"], boost::algorithm::is_digit()) )
Set("count","0");
if ( config["downcount"] == "" || !boost::algorithm::all(config["downcount"], boost::algorithm::is_digit()) )
Set("downcount","0");

}

void Data::Save(){
if( filePath != "" ){
algorithms::Instance().SaveBinary(filePath,&config);
}
}




So it is pretty clear by now that it's either,
tempData failed to initialize
controlParameter is NULL.

but I know that controlParameter is not NULL so it is most likely to be tempData.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
... which you can check by using the debugger ...

Share this post


Link to post
Share on other sites
Anyway, I found out what was causing this

std::ofstream ofs( filePath.c_str() );
{
boost::archive::text_oarchive oa(ofs);
// write class instance to archive
oa << mapData;
}

edit:It was a very simple problem and I freaked out again. Sorry about that.


the path of filePath did not exist, so I had to do a check, if the file exists
ifstream(filePath.cstr()) {
//do whatever
}

Why is the ofstream part of the std when it crashes like that? lol
they should've done a check. no?
please note that I'm not blaming std for this, just curious..

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this