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

风格不统一 提交于 2020-04-18 07:34:12

问题


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 :)


回答1:


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() );


来源:https://stackoverflow.com/questions/23088283/exc-bad-accesscode-1-address-0x0-occurs-when-passing-a-stdmap-as-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!