简体   繁体   English

std::vector 析构函数给出错误

[英]std::vector destructor gives error

I have a class like this:我有一个这样的课程:

class Foo
{
public:
    Foo() {};

    ~Foo() {};

    void MyFunc(int a)
    {
        m_struct.my_vec.push_back(a);
    }

public:
    MyStructType m_struct;
}

and MyStructType is defined similar to this:和 MyStructType 的定义类似于:

struct MyStructType
{
    std::vector<int> my_vec;
}

The problem is that when I instantiate the class as follows, I get a memory violation error for std::vector deallocation when the m_struct destructor is called:问题是,当我按如下方式实例化类时,当调用 m_struct 析构函数时,出现 std::vector 解除分配的内存冲突错误:

void main()
{
    Foo f;
    f.m_struct.my_vec.push_back(10);
}

However, if I do it the following way, the result is the same, but I don't get any error:但是,如果我按照以下方式执行,结果是相同的,但我没有收到任何错误:

int main()
{
    Foo f;
    f.MyFunc(10);
}

To me, the two methods should be identical.对我来说,这两种方法应该是相同的。 Given that the actual code is more complicated than the snippet above, I would prefer to make m_struct public and go with the first option.鉴于实际代码比上面的代码段更复杂,我更愿意公开 m_struct 并使用第一个选项。 Any suggestions as why the first method gives error when vector is being deallocated?关于为什么在释放向量时第一种方法会出错的任何建议?

Thanks!谢谢!

UPDATE: I notices that the problem is in fact in dll_export, which I failed to mention above.更新:我注意到问题实际上出在 dll_export 中,我上面没有提到。 I am generating a dll and using it in another project.我正在生成一个 dll 并在另一个项目中使用它。 If I drop dllexport and put the definitions of the functions (although empty) in the header file, it runs OK.如果我删除 dllexport 并将函数的定义(虽然是空的)放在头文件中,它运行正常。 But when I export my class and put the definitions in the cpp file, it is when it gives me the error.但是当我导出我的类并将定义放在 cpp 文件中时,它给了我错误。 Any ideas?有任何想法吗?

Remove this line : memset(&m_struct, 0, sizeof(m_struct);删除这一行: memset(&m_struct, 0, sizeof(m_struct);

It likely corrupts the vector in MyStructType.它可能会破坏 MyStructType 中的向量。 Why are you doing that ?你为什么这样做?

Since you said you use dll_export, the cause of corruption can be one or more of the following:既然您说您使用 dll_export,那么损坏的原因可能是以下一种或多种:

  • The application and DLL were not compiled with the same compiler options in terms of code generation.在代码生成方面,应用程序和 DLL 没有使用相同的编译器选项进行编译。

  • The application and DLL are using different heaps at runtime.应用程序和 DLL 在运行时使用不同的堆。

For the first item, you must make sure that the compiler options such as structure packing, and any other option that will change the class's internals in some way, are the same.对于第一项,您必须确保编译器选项(例如结构打包)以及任何其他会以某种方式更改类内部结构的选项是相同的。 But even more basic, the app and DLL must be compiled with the same version of the compiler.但更基本的是,应用程序和 DLL 必须使用相同版本的编译器进行编译。

For the second item, you must make sure that the application and DLL use the same heap.对于第二项,您必须确保应用程序和 DLL 使用相同的堆。 To do this, both the DLL and app must use the DLL version of the runtime library.为此,DLL 和应用程序都必须使用运行时库的 DLL 版本。

When you have a class that handles dynamically allocated memory such as std::vector passing and using these class across module boundaries become error prone.当您有一个处理动态分配内存的类时,例如std::vector传递和跨模块边界使用这些类变得容易出错。 Note that the problem is not std::vector , as any class, even one that you would have written, would have the same issue if the class uses the heap.请注意,问题不在于std::vector ,因为如果该类使用堆,则任何类,即使是您编写的类,也会遇到相同的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 构造函数和析构函数中的std :: vector :: clear() - std::vector::clear() in constructor and destructor std :: vector析构函数中的分段错误 - Segmentation fault in std::vector destructor 在从仅移动类型派生的 class 中定义析构函数会在使用 std::vector 的 emplace_back 或 push_back 创建时产生编译时错误 - defining destructor in a class derived from move-only type gives compile-time error when created with emplace_back or push_back of std::vector std::vector 如何处理对析构函数的调用? - how does std::vector deal with the call to destructor? 为优化级别为 0 的 std::vector 调用两次析构函数 - destructor is called twice for std::vector with optimization level 0 可能std :: vector中的元素有抛出析构函数吗? - May the elements in a std::vector have a throwing destructor? std :: vector在重新分配时不调用析构函数 - std::vector doesn't call destructor on reassignment std :: string析构函数中的奇怪错误 - Strange error in std::string destructor 模板类中的析构函数给出错误 - Destructor in template class gives an error 用于初始化std :: vector的初始值设定项列表 <std::function<bool(std::string)> &gt;使用g ++ 4.9.0给出错误,但使用Visual Studio 2013可以正常编译 - initializer list to initialize std::vector<std::function<bool(std::string)> > gives error with g++ 4.9.0 but compiles fine with Visual Studio 2013
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM