分段故障智能指针

时间:2019-07-15 06:25:10

标签: c++11 memory-management smart-pointers

我在访问地址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&标签中的一行中显示错误,               行中的Args && ... args)

autometrics_iter = metrics_.find(哈希);

错误提示 SIGSEV,细分错误 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_a(878(5) ,_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;
  }

Family :: Add函数

 //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命名空间中定义的
  1. GaugeFamilyCopy类有一个名为gauge_dummy的私有成员,其类型为prometheus :: Family *。

  2. 在MySpace命名空间中定义的
  3. GaugeCopy类有一个名为g的私有成员,类型为prometheus :: Gauge *。

  4. 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_;

 };

0 个答案:

没有答案