Click here to Skip to main content
14,486,291 members
Rate this:
Please Sign up or sign in to vote.
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.
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:
typedef std::pair<Cred_t, std::unique_ptr<IAuthenticator>> AuthPair;

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

Lastly the function (defn) in my header file looks like:
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.
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

Rate this:
Please Sign up or sign in to vote.

Solution 1

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.

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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100