EXC_BAD_ACCESS(code=1, address=0x0) occurs when passing a std::map as parameter to a virtual function call

StackOverflow https://stackoverflow.com/questions/23088283

Frage

I have a class that has the following virtual function

namespace Book
{
class Secure
{
virtual Result setPassword(const std::map<std::string, std::vector<std::string> >& params)=0;

}
}

I have another test class in which a method gets the password and user name from the excel and sets it to the map secureParams and sets the password for the current file.

bool initialize(std::string bookPath, std::string loginPath, std::string userName, std::string password)

{
Book::SharedPtr<Book::Secure> secure;
bool result;
std::map<std::string, std::vector<std::string> > secureParams;

std::vector<std::string> userNames;
std::vector<std::string> passwords;

userNames.push_back(userName);
passwords.push_back(password);

secureParams.insert(std::pair<std::string, std::vector<std::string>>("USERNAME",userNames);
secureParams.insert(std::pair<std::string, std::vector<std::string>>("PASSWORD",passwords);

secure->setPassword(secureParams);

secure->getLoginFile(loginPath.c_str());

result=Book::SecureBook::canLogin(bookPath.c_str(),secure);

return result;

}

The code when run terminates saying Unknown File: Failure Unknown C++ exception thrown in test body.

When debugged in XCode it shows a EXC_BAD_ACCESS(code=1,address=0xc0000000) error in the line secure->setPassword(secureParams);

I have been trying to debug the issue all day. Any help is appreciated :)

Thanks in advance :)

War es hilfreich?

Lösung

You don't seem to have an object that inherits from Book::Secure, just a smart pointer to the abstract base class. When you dereference the smart pointer that has never been set to point to an actual object, you get a crash.

Clarification:

1) You need an instantiable class that inherits from Book::Secure

namespace Book
{

class Foo : public Secure
{
public:
virtual Result setPassword(const std::map<std::string, std::vector<std::string> >& params) {
  cout << "setPassword called" << endl; 
}
}
}

2) You need to instantiate the class and make your smart pointer point to it before you use the smart pointer:

secure.reset( new Foo() );
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top