DLL / SO 库,库内存与调用进程有何关系?

发布于 2024-12-01 18:24:11 字数 278 浏览 1 评论 0原文

我读到,当进程终止(以任何方式)时,操作系统会释放所有进程的内存,因此无需依次调用每个 dtor。

现在我的问题是 DLL 或 SO 的内存与分配内存的清理有何关系?

我问这个问题是因为我最终可能会使用 Java 和/或 C# 来调用带有一些静态 C 风格函数的 C++ DLL,这些函数将在堆上分配 C++ 对象。抱歉,如果我对堆线程与堆栈线程得意忘形,我觉得我已经忘记了__堆的概念(即只有一个)。

使用库时还有其他潜在的内存泄漏陷阱吗?

I was reading that all a process's memory is released by the OS when the process terminates (by any means) so negating the need to call every dtor in turn.

Now my question is how does the memory of a DLL or SO relate to clean up of alloc'd memory?

I ask because I will probably end up using a Java and/or C# to call into a C++ DLL with some static C style functions which will allocate the C++ objects on the heap. Sorry if I got carried away with the heap vs stack thread, I feel I have lost sight of the concept of _the_ heap (ie only one).

Any other potential pitfalls for memory leaks when using libraries?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

感情废物 2024-12-08 18:24:11

加载库后,它就成为进程的一部分。对于内存、句柄、资源等的整理,系统不区分它们是在可执行映像还是库中创建的。

The library becomes part of the process when it is loaded. Regarding tidy up of memory, handles, resources etc., the system doesn't distinguish whether they were created in the executable image or the library.

擦肩而过的背影 2024-12-08 18:24:11

您无需担心。操作系统的加载程序负责处理这个问题。

一般来说,共享库将通过内存映射(全部由加载程序完成)对进程的地址空间可见,并且操作系统会跟踪有多少进程仍然需要给定的共享库。每个进程单独需要的状态数据通常通过写时复制来处理,因此您的加密库不会有意外使用另一个进程的密钥的危险:-) 简而言之,不用担心。

编辑。也许您想知道如果您的库函数调用 malloc() 并且不进行清理会发生什么。好吧,库的代码成为您进程的一部分,因此实际上是您的进程请求内存,因此当您的进程终止时,操作系统会像往常一样进行清理。

There is nothing for you to worry about. The operating system's loader takes care of this.

In general, shared libraries will be made visible to your process's address space via memory mapping (all done by the loader), and the OS keeps track of how many processes still need a given shared library. State data that is needed separately per process is typically handled by copy-on-write, so there's no danger that your crypto library might accidentally be using another process's key :-) In short, don't worry.

Edit. Perhaps you're wondering what happens if your library function calls malloc() and doesn't clean up. Well, the library's code becomes part of your process, so it is really your process that requests the memory, and so when your process terminates, the OS cleans up as usual.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文