Windows/Linux中的析构函数调用顺序 [英] Destructor call order in Windows/Linux

查看:122
本文介绍了Windows/Linux中的析构函数调用顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码在Windows上可以正常运行,但是在Linux上由于分段错误而崩溃.

I had the following code that worked fine on Windows, but would crash on Linux with a segmentation fault.

{
char memory[1024];
Object(memory);
}



对象将使用内存进行操作.关于崩溃发生的原因,我有两种理论:

1)在Linux和Windows中,析构函数的调用顺序不同,在Windows上,在更改内存"之前先调用析构函数.
2)Linux以某种不允许对象析构函数正常工作的方式标记内存"空间.

我已经解决了new和delete的问题,但想知道为什么这个问题在一个平台而不是另一个平台上显现出来.使用的编译器是用于Linux的C ++(GCC 4.2.3 x86)和友好的VS编译器6.0.对于Windows.



Object would use the memory for its operations. I have two theories about why the crash happened:

1) Destructor call order is different in Linux and Windows, on Windows Object destructor is called before making changes to "memory".
2) Linux marks the "memory" space in some manner that doesn''t allow Object destructor to work properly.

I have already fixed the problem with new and delete, but would like to know why this problem manifested itself on one platform and not the other. Used compilers are C++ (GCC 4.2.3 x86) for Linux and your friendly neighborhood VS compiler 6.0. for Windows.

推荐答案

C ++析构函数调用顺序由C ++标准指定.这不是特定于操作系统的.
C++ destructor call order is specified by the C++ standard. That will not be operating system specific.


销毁的顺序不能由编译器编写者掌握,它是由标准设置的.如果您有两个对象a和b,则声明为:

Order of destruction isn''t up for grabs by compiler writers - it''s set by the standard. If you have two objects a and b, declared as:

A a;
B b;



那么总是会首先调用b的析构函数.因此,如果b依赖于a,它将始终在其依赖对象之前被销毁.

因此,您需要问自己的是...您正在采取什么狡猾的做法,这会杀死该过程?我更倾向于说VC ++ 6可能是罪魁祸首-它是一个过时的编译器,并不是特别擅长做标准的事情,并且可以让您摆脱各种各样的垃圾.相对较新的编译器(如gcc 4.x)不会.

所以...

-将两个编译器上的警告都提高到/W4/WX
-获取新版本的VC ++-下载速成版并尝试使用该版本进行编译
-开始编写单元测试,以找出您的课堂出了问题以及在什么情况下出错.例如传递随机填充的字符数组,零指针,指向全零数组的指针时,测试类的行为.

除了您没有描述对象"的功能之外,我再没有比这更具体的内容了,只是为了某些邪恶的目的而使用了一个字符块.

干杯,

Ash



then the destructor of b is always going to be called first. So if b depends on a it will always be destroyed before its dependent object.

So what you''ve got to ask yourself is... what dodgy practice are you doing that will kill the process? I''d be more inclined to say that VC++6 is likely to be the culprit - it''s an antique compiler that isn''t particularly good at doing standard things and lets you get away with all sorts of rubbish that a relatively modern compiler (like gcc 4.x) won''t.

So...

- crank up the warnings on both compilers to /W4 /WX
- get a new version of VC++ - download the express edition and try compiling with that
- start writing unit tests to find out where you class goes wrong and under what circumstances. e.g. test the behaviour of the class when passed a randomly filled array of characters, a zero pointer, a pointer to an array of all zeros.

I can''t be anymore specific than that as you haven''t described what "Object" does except for taking a block of characters for some nefarious end.

Cheers,

Ash


这实际上是由于堆栈太小所致.
This was in fact caused by the stack being too small.


这篇关于Windows/Linux中的析构函数调用顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆