Click here to Skip to main content
15,896,201 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to construct a hash_map of values, key -> unique pointer, the code compiles fine. However on every execution I get an error from the STL about an access violation with iterators and I am not sure why.

The offending code looks like.
C++
void AuthenticationRequestDelegator::registerAuthenticator(std::unique_ptr<IAuthenticator> authenticator)
{
	Cred_t type = authenticator->getCredentialType();
	m_authenticators.insert(AuthPair(type, std::move(authenticator)));
}

With an AuthPair defined as:
C++
typedef std::pair<Cred_t, std::unique_ptr<IAuthenticator>> AuthPair;


The hash map is defined as:
C++
typedef std::hash_map<Cred_t, std::unique_ptr<IAuthenticator>> AuthenticatorList;
AuthenticatorList m_authenticators;


Lastly the function (defn) in my header file looks like:
C++
void registerAuthenticator(std::unique_ptr<IAuthenticator> authenticator);


The m_authenticators hash_map blows up every insert with an access violation. Any light would be appreciated.
Posted
Comments
KarstenK 7-Dec-14 13:21pm    
m_authenticators.insert(AuthPair(type, std::move(authenticator)));

split up this line. Is the std::move() correct?

Looks like temp objects :-/
CdnSecurityEngineer 7-Dec-14 13:34pm    
It has something to do with the fact that, my hash map is declared as a member variable of my class, which I really can't say I understand. For example if I change it to be a hashmap of <int,int> then I get the same access violation, however, if the hash map is declared inside the function, ergo on the stack, then everything works out fine.

Therefore it seems to be an issue with the fact the hash_map is declared at the class level which I don't get.

1 solution

The issue was with the way AuthenticationRequestDelegator was being authenticated. Essentially a bald pointer was being created, which was allowing the constructor to fire, and then when registerAuthenticator was being called this was NULL. Which essentially means the pointer calling this function had no instance reference, and therefor the hash_map itself was null, despite the fact it looked like it was being instantiated. Ensuring the correct use of std::move instead of relying on a copy construction resolved this issue.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900