本文介绍了std :: vector析构函数给出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的类:

  class Foo 
{
public:
Foo(){};

〜Foo(){};

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

public:
MyStructType m_struct;
}

,MyStructType的定义类似于:

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

问题是当我实例化类如下,当调用m_struct析构函数时,std :: vector释放的违规错误:

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

但是,如果我这样做,结果是一样的,但我没有得到任何错误:

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

对我来说,这两种方法应该是相同的。鉴于实际代码比上面的代码段更复杂,我更喜欢使m_struct公开和去第一个选项。任何建议为什么第一个方法在向量被解除分配时给出错误?



谢谢!



更新:
我注意到,问题实际上是在dll_export,我上面没有提到。我生成一个dll,并在另一个项目中使用它。如果我删除dllexport并将函数的定义(虽然为空)放在头文件中,它运行确定。但是当我导出我的类并把定义放在cpp文件中时,它是它给我的错误。任何想法?

解决方案

由于你说你使用dll_export,腐败的原因可以是以下一个或多个: p>


  • 在代码生成方面,应用程序和DLL没有使用相同的编译器选项进行编译。


  • 应用程序和DLL在运行时使用不同的堆。




对于第一个项目,必须确保编译器选项(如结构打包)将以某种方式改变类的内部,是一样的。但是更基本的是,应用程序和DLL必须使用相同版本的编译器进行编译。



对于第二个项目,必须确保应用程序和DLL使用相同的堆。要执行此操作,DLL和应用程序必须使用运行时库的DLL版本。



当你有一个类处理动态分配的内存,如 std :: vector 在模块边界传递和使用这些类变得容易出错。注意,问题不是 std :: vector ,因为任何类,即使是你会写的类,如果类使用堆,也会有同样的问题。 p>

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:

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:

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. 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. I am generating a dll and using it in another project. If I drop dllexport and put the definitions of the functions (although empty) in the header file, it runs OK. But when I export my class and put the definitions in the cpp file, it is when it gives me the error. Any ideas?

解决方案

Since you said you use dll_export, the cause of corruption can be one or more of the following:

  • The application and DLL were not compiled with the same compiler options in terms of code generation.

  • The application and DLL are using different heaps at runtime.

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.

For the second item, you must make sure that the application and DLL use the same heap. To do this, both the DLL and app must use the DLL version of the runtime library.

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. 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析构函数给出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-25 03:46