c++ - 使用静态映射和 std::make_pair 的地址处的 KERN_PROTECTION_FAILURE

标签 c++ multithreading segmentation-fault exc-bad-access

我收到这个错误

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_PROTECTION_FAILURE at address: 0xb064425c

调用以下函数时:

const std::map<FunctionID, std::string> getEnumElementsStringRepresentation() {
  static bool is_initialized = false;
  static std::map<FunctionID, std::string> enum_string_representation; // Fails here
  if (false == is_initialized) {
    enum_string_representation.insert(std::make_pair(FunctionID.capabilities, "capabilities"));
    ... many more similar inserts
    is_initialized = true;
  }
  return enum_string_representation;
}

这似乎是一个读/写错误,但我不确定调试的最佳方法。

最佳答案

你说你的代码是多线程的。由于函数静态 map 实例,getEnumElementsStringRepresentation 不可重入(通常)。您将不得不使用互斥锁来保护函数,或者使用线程本地存储。

关于c++ - 使用静态映射和 std::make_pair 的地址处的 KERN_PROTECTION_FAILURE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22673292/

相关文章:

c++ - C++中的字符串 View

c++ - 使用 Python 访问 USB 硬件(具有 native C++ API 的压力传感器矩阵)

c++ - 数据模型问题

c++ - 多线程独立任务

c - 运行时错误 "Segmentation Fault"。如何去除它? (我在 Ubuntu 中工作。)

c++ - 某种类型别名 : An empty derived class

java - 如何在 Java 中运行同一进程的多个线程 - Spring Boot

java - ThreadWeaver 总是抛出 IllegalArgumentException

c++ - 等待调试器崩溃?

C fscanf 段错误错误 "No source available for "fancyfile() at 0x7fff855e6d39"