毕竟“_atexit()”之后我怎样才能安排一些代码运行呢?功能已完成
我正在编写一个内存跟踪系统,我实际遇到的唯一问题是,当应用程序退出时,任何未在构造函数中分配但在解构函数中释放的静态/全局类都会在我的内存之后释放跟踪人员已将分配的数据报告为泄漏。
据我所知,正确解决此问题的唯一方法是强制将内存跟踪器的 _atexit 回调放置在堆栈的头部(以便最后调用它)或让它在整个堆栈之后执行_atexit 堆栈已被展开。实际上是否可以实现这些解决方案中的任何一个,或者是否有另一个我忽略的解决方案。
编辑: 我正在为 Windows XP 进行开发并使用 VS2005 进行编译。
I'm writing a memory tracking system and the only problem I've actually run into is that when the application exits, any static/global classes that didn't allocate in their constructor, but are deallocating in their deconstructor are deallocating after my memory tracking stuff has reported the allocated data as a leak.
As far as I can tell, the only way for me to properly solve this would be to either force the placement of the memory tracker's _atexit callback at the head of the stack (so that it is called last) or have it execute after the entire _atexit stack has been unwound. Is it actually possible to implement either of these solutions, or is there another solution that I have overlooked.
Edit:
I'm working on/developing for Windows XP and compiling with VS2005.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我已经读过多次,你不能保证全局变量的构造顺序(引用)。我认为可以很安全地推断析构函数的执行顺序也不能得到保证。
因此,如果您的内存跟踪对象是全局的,您几乎肯定无法保证您的内存跟踪对象将最后被破坏(或首先被构造)。如果它最后没有被破坏,并且其他分配尚未完成,那么它会注意到您提到的泄漏。
另外,这个 _atexit 函数是为什么平台定义的?
I've read multiple times you can't guarantee the construction order of global variables (cite). I'd think it is pretty safe to infer from this that destructor execution order is also not guaranteed.
Therefore if your memory tracking object is global, you will almost certainly be unable any guarantees that your memory tracker object will get destructed last (or constructed first). If it's not destructed last, and other allocations are outstanding, then yes it will notice the leaks you mention.
Also, what platform is this _atexit function defined for?
最后执行内存跟踪器的清理是最好的解决方案。我发现最简单的方法是显式控制所有相关全局变量的初始化顺序。 (有些库将其全局状态隐藏在奇特的类中或以其他方式,认为它们遵循某种模式,但它们所做的只是阻止这种灵活性。)
示例 main.cpp:
其中全局初始化文件包含对象定义和 #includes类似的非头文件。按照您希望构造的顺序排列此文件中的对象,它们将以相反的顺序被破坏。 C++03 中的 18.3/8 保证销毁顺序镜像构造:“具有静态存储持续时间的非本地对象按照其构造函数完成的相反顺序销毁。” (该部分讨论的是
exit()
,但是从 main 的返回是相同的,请参阅 3.6.1/5。)作为奖励,您可以保证所有全局变量(在该文件中)在进入 main 之前进行初始化。 (标准中没有保证某些内容,但如果实现选择则允许。)
Having the memory tracker's cleanup executed last is the best solution. The easiest way I've found to do that is to explicitly control all the relevant global variables' initialization order. (Some libraries hide their global state in fancy classes or otherwise, thinking they're following a pattern, but all they do is prevent this kind of flexibility.)
Example main.cpp:
Where the global-initialization file includes object definitions and #includes similar non-header files. Order the objects in this file in the order you want them constructed, and they'll be destructed in the reverse order. 18.3/8 in C++03 guarantees that destruction order mirrors construction: "Non-local objects with static storage duration are destroyed in the reverse order of the completion of their constructor." (That section is talking about
exit()
, but a return from main is the same, see 3.6.1/5.)As a bonus, you're guaranteed that all globals (in that file) are initialized before entering main. (Something not guaranteed in the standard, but allowed if implementations choose.)
我也遇到过这个问题,还写了一个内存跟踪器。
有几件事:
除了破坏之外,您还需要处理建设。在构建内存跟踪器之前,准备好调用 malloc/new(假设它是作为类编写的)。所以你需要你的类知道它是否已经被构造或销毁了!
在调用跟踪器的每个分配上,构建它!
奇怪,但却是事实。无论如何,到破坏:
所以,在破坏时,输出你的结果。但我们知道还会有更多的电话。该怎么办?好吧,...
最后:
编辑:
I've had this exact problem, also writing a memory tracker.
A few things:
Along with destruction, you also need to handle construction. Be prepared for malloc/new to be called BEFORE your memory tracker is constructed (assuming it is written as a class). So you need your class to know whether it has been constructed or destructed yet!
On every allocation that calls into your tracker, construct it!
Strange, but true. Anyhow, onto destruction:
So, on destruction, output your results. Yet we know that there will be more calls. What to do? Well,...
And lastly:
EDIT:
我终于弄清楚如何在 Windows/Visual Studio 下执行此操作。再次查看 crt 启动函数(特别是它调用全局变量初始化程序的地方),我注意到它只是运行包含在某些段之间的“函数指针”。因此,只要了解一点关于链接器如何工作的知识,我就想出了这个:
输出:
这是由于 MS 编写运行时库的方式而起作用的。基本上,他们在数据段中设置了以下变量:(
尽管此信息受版权保护,但我相信这是合理使用,因为它不会贬低原始数据,并且仅在此供参考)
在初始化时,程序只是从 ' 进行迭代__xN_a' 到 '__xN_z'(其中 N 是 {i,c,p,t})并调用它找到的任何非空指针。如果我们只是在段 '.CRT$XnA' 和 '.CRT$XnZ' 之间插入我们自己的段(其中 n 再次是 {I,C,P,T}),它将与其他所有内容一起被调用通常会被调用。
链接器只是按照字母顺序将各个段连接起来。这使得选择何时调用我们的函数变得非常简单。如果您查看
defsects.inc
(位于$(VS_DIR)\VC\crt\src\
下),您可以看到 MS 已放置所有“用户”以“U”结尾的段中的初始化函数(即在代码中初始化全局变量的函数)。这意味着我们只需将初始化器放在“U”之前的段中,并且它们将在任何其他初始化器之前被调用。您必须非常小心,不要使用任何在您选择的函数指针位置之后才初始化的功能(坦率地说,我建议您只使用
.CRT$XCT
这样它就只是您的我不确定如果您与标准“C”代码链接会发生什么,您可能必须将其放在其中的.CRT$XIT
块中。案件)。我确实发现的一件事是,如果链接到运行时库的 DLL 版本,“预终止符”和“终止符”实际上并未存储在可执行文件中。因此,您不能真正将它们用作通用解决方案。相反,我让它运行我的特定函数作为最后一个“用户”函数的方法是简单地在“C 初始化程序”中调用
atexit()
,这样,就不能添加其他函数了堆栈(将以与添加函数相反的顺序调用,并且是全局/静态解构函数的调用方式)。最后(显而易见的)一点是,本文是在考虑到 Microsoft 运行时库的情况下编写的。它在其他平台/编译器上的工作方式可能类似(希望您只需将段名称更改为它们使用的任何名称即可,如果它们使用相同的方案),但不要指望它。
I've finally figured out how to do this under Windows/Visual Studio. Looking through the crt startup function again (specifically where it calls the initializers for globals), I noticed that it was simply running "function pointers" that were contained between certain segments. So with just a little bit of knowledge on how the linker works, I came up with this:
which outputs:
This works due to the way MS have written their runtime library. Basically, they've setup the following variables in the data segments:
(although this info is copyright I believe this is fair use as it doesn't devalue the original and IS only here for reference)
On initialization, the program simply iterates from '__xN_a' to '__xN_z' (where N is {i,c,p,t}) and calls any non null pointers it finds. If we just insert our own segment in between the segments '.CRT$XnA' and '.CRT$XnZ' (where, once again n is {I,C,P,T}), it will be called along with everything else that normally gets called.
The linker simply joins up the segments in alphabetical order. This makes it extremely simple to select when our functions should be called. If you have a look in
defsects.inc
(found under$(VS_DIR)\VC\crt\src\
) you can see that MS have placed all the "user" initialization functions (that is, the ones that initialize globals in your code) in segments ending with 'U'. This means that we just need to place our initializers in a segment earlier than 'U' and they will be called before any other initializers.You must be really careful not to use any functionality that isn't initialized until after your selected placement of the function pointers (frankly, I'd recommend you just use
.CRT$XCT
that way its only your code that hasn't been initialized. I'm not sure what will happen if you've linked with standard 'C' code, you may have to place it in the.CRT$XIT
block in that case).One thing I did discover was that the "pre-terminators" and "terminators" aren't actually stored in the executable if you link against the DLL versions of the runtime library. Due to this, you can't really use them as a general solution. Instead, the way I made it run my specific function as the last "user" function was to simply call
atexit()
within the 'C initializers', this way, no other function could have been added to the stack (which will be called in the reverse order to which functions are added and is how global/static deconstructors are all called).Just one final (obvious) note, this is written with Microsoft's runtime library in mind. It may work similar on other platforms/compilers (hopefully you'll be able to get away with just changing the segment names to whatever they use, IF they use the same scheme) but don't count on it.
atexit 由 C/C++ 运行时 (CRT) 处理。它在 main() 返回后运行。也许最好的方法就是用您自己的 CRT 替换标准 CRT。
在 Windows 上,tlibc 可能是一个很好的起点:http://www.codeproject.com /KB/library/tlibc.aspx
查看 mainCRTStartup 的代码示例,然后在调用 _doexit() 后运行代码;
但在 ExitProcess 之前。
或者,您可以在调用 ExitProcess 时收到通知。当 ExitProcess 被调用时,会发生以下情况(根据 http: //msdn.microsoft.com/en-us/library/ms682658%28VS.85%29.aspx):
因此,一种方法是创建一个 DLL 并将该 DLL 附加到进程。当进程退出时,它会收到通知,这应该是在 atexit 被处理之后。
显然,这一切都相当黑客,请小心行事。
atexit is processed by the C/C++ runtime (CRT). It runs after main() has already returned. Probably the best way to do this is to replace the standard CRT with your own.
On Windows tlibc is probably a great place to start: http://www.codeproject.com/KB/library/tlibc.aspx
Look at the code sample for mainCRTStartup and just run your code after the call to _doexit();
but before ExitProcess.
Alternatively, you could just get notified when ExitProcess gets called. When ExitProcess gets called the following occurs (according to http://msdn.microsoft.com/en-us/library/ms682658%28VS.85%29.aspx):
So, one method would be to create a DLL and have that DLL attach to the process. It will get notified when the process exits, which should be after atexit has been processed.
Obviously, this is all rather hackish, proceed carefully.
这取决于开发平台。例如,Borland C++ 有一个 #pragma 可以用来完成此任务。 (来自 Borland C++ 5.0,约 1995 年)
也许您的 C 编译器有类似的功能?
This is dependent on the development platform. For example, Borland C++ has a #pragma which could be used for exactly this. (From Borland C++ 5.0, c. 1995)
Perhaps your C compiler has a similar facility?