c++ - Release模式下 OSX/AppleClang 上的 boost::container::flat_multimap 崩溃

标签 c++ macos boost

我在 MacOS/AppleClang 上的 Release 模式 (-O3) 上似乎遇到了 boost::container::flat_multimap (Boost 1.66.0) 的问题。我在 Ubuntu 17.10/GCC7.2 和 Oracle Linux/GCC7.2.1 中测试,并没有出现问题。

下面是一个最小的可重现示例。

代码:

#include <iostream>
#include <vector>

#include <boost/container/flat_map.hpp>

using multimap = boost::container::flat_multimap<int *, int>;

int main(int argc, char **argv)
{
    multimap map;
    std::vector<std::pair<int *, int>> key_value_pairs;

    for (int k = 0; k < 2; k++) {
        int * new_int = new int;
        *new_int = k;
        map.emplace(new_int, k);
        key_value_pairs.emplace_back(new_int, k);
    }

    for (auto it = map.begin(); it != map.end();) {
        if (it->first == key_value_pairs[0].first) {
            it = map.erase(it);
        } else {
            ++it;
        }
    }

    // Should only be one map element left (key_value_pairs[1])
    auto it = map.find(key_value_pairs[1].first);
    if (it == map.end()) {
        throw std::logic_error("Could not find key");
    }

    std::cout  << "Success!" << std::endl;
    return EXIT_SUCCESS;
}

叮当版

hoc$ clang --version
Apple LLVM version 9.0.0 (clang-900.0.39.2)
Target: x86_64-apple-darwin17.4.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

在这个例子中,我们生成了 2 个 int 指针并将它们作为键插入到 boost::container::flat_multimap 中。然后我们遍历映射并通过识别键删除其中一个条目。随后尝试按键查找未删除的元素,但有时找不到(有时会触发第31行的std::logic_error)。

我的代码中有错误吗?还是容器?

最佳答案

我没有看到代码有任何实际/错误/。

有相当多的代码味道 - 这里的“等效”程序是否显示相同的问题?

Live On Coliru

#include <boost/container/flat_map.hpp>
#include <iostream>
#include <vector>

using multimap = boost::container::flat_multimap<int *, int>;

int main() {
    int ks[] = { 0, 1 }; // allocation pool outlives map and key_value_pairs

    multimap map;
    for (int &k : ks)
        map.emplace(&k, k);

    std::vector<multimap::value_type> const key_value_pairs(map.begin(), map.end());

    map.erase(key_value_pairs[0].first);

    assert(map.size() == 1);
    assert(map.begin()->first == key_value_pairs[1].first);

    assert(map.count(&ks[0]) == 0);
    assert(map.count(&ks[1]) == 1);
}

关于c++ - Release模式下 OSX/AppleClang 上的 boost::container::flat_multimap 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48912062/

相关文章:

c++ - 将 C++11 lambda 与 boost::multi_index 结合使用

c++ - 错误: Qualified reference to 'Complex' is a constructor name rather than a type in this context

C++ 如何将可变参数函数模板的参数包 -> 包装到 lambda 中

xcode - 在较早的osx版本中,从xcassets加载带有imageNamed的NSImage会崩溃

python - 根据一组坐标的数据对 map 进行着色

macos - 在 OSX 中,访问(可选?) NSView 子类的属性 "identifier"会导致访问错误

c++ - 是否返回 void 有效代码?

c++ - 字符串流问题,我认为我的代码看起来不错,但它显示奇怪的符号

c++ - 使用 Boost assign 初始化具有变量值的类 JSON 映射

c++ - 使用 R CMD SHLIB 编译 C++ 文件时如何指定标题搜索路径?