gpt4 book ai didi

c++ - Google Test 中的 RAII 内存损坏

转载 作者:行者123 更新时间:2023-11-30 05:21:35 24 4
gpt4 key购买 nike

我已经为 C 指针实现了一个自动删除器。该代码在测试程序中有效,但是当我在 Google Test 中使用该代码时,奇怪的事情发生了。我不明白为什么。我写过未定义的行为吗?还是 Google Test 会以某种方式干扰?

下面的代码,如果宏 ASSERT_THAT 被注释掉,打印:

i1 = 0x8050cf0
i2 = 0x8050d00
got: 0x8050cf0
got: 0x8050d00
go delete: 0x8050cf0
go delete: 0x8050d00

创建了两个指针,守卫获取这些指针并稍后将其删除。到目前为止完全符合要求。

如果宏处于事件状态,则结果为:

i1 = 0x8054cf0
i2 = 0x8054d00
got: 0x8054cf0
got: 0x8054d00
go delete: 0x8054c01

出于某种原因,代码删除了另一个指针,然后删除了一个指针。我完全糊涂了。你能帮忙找出问题所在吗?

#include <iostream>
#include <gmock/gmock.h>

using namespace testing;

class Scope_Guard {
public:
Scope_Guard(std::initializer_list<int*> vals)
: vals_(vals)
{
for (auto ptr: vals_) {
std::cerr << "got: " << ptr << std::endl;
}
}
~Scope_Guard() {
for (auto ptr: vals_) {
std::cerr << "go delete: " << ptr << std::endl;
delete ptr;
}
}
Scope_Guard(Scope_Guard const& rhs) = delete;
Scope_Guard& operator=(Scope_Guard rhs) = delete;
private:
std::initializer_list<int*> vals_;
};

TEST(Memory, GuardWorksInt) {
int* i1 = new int(1);
int* i2 = new int(2);
std::cerr << "i1 = " << i1 << std::endl;
std::cerr << "i2 = " << i2 << std::endl;
Scope_Guard g{i1, i2};

ASSERT_THAT(1, Eq(1)); // (*)
}

int main(int argc, char** argv) {
InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

最佳答案

这是未定义的行为:

您正在将构造函数参数中的 std::initializer_list 复制到类成员中。

复制 std::initializer_list 不会复制其底层元素。因此,在离开构造函数后,无法保证 vals_ 包含任何有效内容。

为成员使用 std::vector 并从初始化列表构造它。

我不确定你对这个守卫的意图,但使用 std::unique_ptr 可能会更容易。

关于c++ - Google Test 中的 RAII 内存损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40012464/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com