c++11 - 分段错误智能指针
问题描述
I get an error of" Segmentation fault while accessing address 0x0000000000000078 .(_ZNKSt8_detail15_Hash_code_baseImSt4pairIKmSt10unique_ptrIN10prometheus5GaugeESt14default_deleteIS5_EEENS_10_Select1stEst4hashImENS_18_Mod_range_hashingENS_20_Default_ranged_hashELb0EE15_M_bucket_indexEPKNS_10_Hash_nodeIS9_Lb0EEEm+0x3b)[0x7fff84143cac1]
在调试时,它在函数 Family::Add(const std::map& labels, Args&&... args) 的行中显示错误->
自动 metrics_iter = metrics_.find(hash);
错误表示 SIGSEV,Gegmentation fault 0x00007fffcfa29ac1 in std::detail::_Hash_code_base>>,std::_detail::_Select1st,std::hash,std::_detail::_Mod_range_hashing,std::detail::_Default_ranged_hash,false> ::_M_bucket_index (this=0x86a5878, _p=0x70,_n=1)include/c++/6.3.1/bits/hashtable_policy.h
代码如下->
GaugeFamilyCopy::Add() 函数
//namespace prometheus
GaugeCopy* GaugeFamilyCopy::Add()
{ GaugeCopy* ge = new GaugeCopy(gauge_family->Add(mp));
return ge;
}
家庭::添加功能
//namespace prometheus
T& Family<T>::Add(const std::map<std::string, std::string>& labels,
Args&&... args) {
auto hash = detail::hash_labels(labels);
std::lock_guard<std::mutex> lock{mutex_};
auto metrics_iter = metrics_.find(hash);
/*some code which returns T& which I didn't include as the error lies before
it */
}
}
hash_labels 函数
//namespace detail which is inside prometheus namespace
std::size_t hash_labels(const std::map<std::string, std::string>& labels)
{
size_t seed = 0;
for (auto& label : labels) {
hash_combine(&seed, label.first, label.second);
}
return seed;
}
我还分配了 gauge_family 它在这个函数中的值->
//namespace MySpace
void GaugeFamilyCopy::MakeGauge2(std::string s1, std::string s2, const
std::map<string,string>& labels, MyExposer* ex)
{ auto registry = make_shared<prometheus::Registry>();
gauge_family = & (prometheus::BuildGauge().Name(s1).Help(s2).Labels(labels).Register(*registry));
}
下面给出了注册函数->
//namespace detail nested inside namespace prometheus
Family<Gauge>& GaugeBuilder::Register(Registry& registry) {
return registry.Add<Gauge>(name_, help_, labels_);
}
上面使用的 Add 函数是 -
template <typename T>
Family<T>& Registry::Add(const std::string& name, const std::string& help,
const std::map<std::string, std::string>& labels) {
std::lock_guard<std::mutex> lock{mutex_};
auto family = detail::make_unique<Family<T>>(name, help, labels);
auto& ref = *family;
collectables_.push_back(std::move(family));
return ref;
}
在 MySpace 命名空间中定义的 GaugeFamilyCopy 类有一个名为 gauge_dummy 的私有成员,其类型为 prometheus::Family*。
在 MySpace 命名空间中定义的 GaugeCopy 类有一个名为 g 的私有成员,其类型为 prometheus::Gauge*。
Gauge 是在 prometheus 命名空间中定义的一个类。
家庭班
//namespace prometheus
template <typename T>
class Family : public Collectable {
public:
Family(const std::string& name, const std::string& help,
const std::map<std::string, std::string>& constant_labels);
private:
std::unordered_map<std::size_t, std::unique_ptr<T>> metrics_;
std::unordered_map<std::size_t, std::map<std::string, std::string>>
labels_;
std::mutex mutex_;
};
解决方案
推荐阅读
- c - 使用嵌套的 for 循环在 C 语言中打印数字矩阵模式高级/中等调试
- html - 如何使标题徽标响应更大的屏幕宽度?
- ios - 铸造一个泛型类
- swagger - 如何在 Loopback 4 的 Swagger 中添加“授权标头”输入?
- javascript - Javascript 按日期过滤 JSON
- java - 为什么 Spring 强调紧密耦合,尽管它可以通过编程方式克服?
- r - R:R中的“折叠”行
- python-3.x - python derived class constructor
- azure - Azure Application Insight:将遥测流式传输到文件
- javascript - 代码中的哪个函数有错误以及如何修复代码以再次正常工作