为什么在插入指向hash_map的指针时会出现访问冲突 [英] Why do I get Access Violation when Inserting pointer to hash_map

查看:83
本文介绍了为什么在插入指向hash_map的指针时会出现访问冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构造值的hash_map,key - >独特的指针,代码编译好。但是在每次执行时,我从STL收到有关迭代器访问冲突的错误,我不知道为什么。



违规代码如下。

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



将AuthPair定义为:


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;





最后我头文件中的函数(defn)如下:



Lastly the function (defn) in my header file looks like:

void registerAuthenticator(std::unique_ptr<IAuthenticator> authenticator);





m_authenticators hash_map会炸毁每个带有访问冲突的插入。任何亮点都会被赞赏。



The m_authenticators hash_map blows up every insert with an access violation. Any light would be appreciated.

推荐答案

问题在于验证AuthenticationRequestDelegator的方式。本质上是一个正在创建的光头指针,它允许构造函数触发,然后当调用registerAuthenticator时,这是NULL。这本质上意味着调用此函数的指针没有实例引用,因此hash_map本身为null,尽管事实上它看起来像是在实例化。确保正确使用std :: move而不是依赖于复制结构解决了这个问题。
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.


这篇关于为什么在插入指向hash_map的指针时会出现访问冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆