我需要删除静态 std::map 吗?

发布于 2024-10-13 02:25:29 字数 119 浏览 3 评论 0原文

在某些类中,我有一个内部有指针的静态 std::map 。我的问题是我是否需要在程序结束时删除或者该内存会自动释放。我担心的是,当删除 std::map 时,存储在内部的指针是否通过析构函数正确删除。

谢谢。

In some classes I have an static std::map with pointers inside. My question is if I need to delete at the end of the program or this memory is automatically freed. My concern is if the pointers stored inside are correctly deleted through our destructors when std::map is deleted.

Thanks.

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

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

发布评论

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

评论(7

白鸥掠海 2024-10-20 02:25:29

如果映射包含使用 new (或 < a href="http://www.cplusplus.com/reference/std/new/operator%20new%5B%5D/" rel="nofollow noreferrer">new[],或 malloc),那么每个指针都需要一个对应的 删除 (或 删除[],或 免费)。

地图的析构函数不知道如何处理光头指针。考虑使用具有适当移动语义的 智能指针,例如 boost 智能指针 或者如果您有一个非常新的编译器,< a href="http://en.wikipedia.org/wiki/C%2B%2B0x#General- Purpose_smart_pointers" rel="nofollow noreferrer">C++0x 智能指针。但是,不要使用当前标准的 std::auto_ptr ,STL 容器内部。 查看此帖子了解原因

编辑:

正如比利·奥尼尔指出的那样, boost::ptr_map 也正是为此目的而设计的。

If the map contains pointers that were allocated with new (or new[], or malloc), then each pointer needs a corresponding delete (or delete[], or free).

The map's destructor wont know what to do with a bald pointer. Consider using a smart pointer that has appropriate move semantics like a boost smart pointer or if you've got a very new compiler, one of the C++0x smart pointers. However, do not use the current standard's std::auto_ptr, inside of STL containers. See this thread for why.

Edit:

As Billy ONeal pointed out, boost::ptr_map is also designed exactly for this purpose.

晨光如昨 2024-10-20 02:25:29

如果我对情况的理解正确的话,你不会删除地图本身。但您可能需要删除地图指向的对象。使用智能指针可能是一个非常好的主意,例如 在你的地图中提升shared_ptr而不是原生指针。然后物体就会被自动清理。

编辑:
使用 Boost ptr_map 可能是一个更好的主意。

If I understand the situation correctly, you don't delete the map itself. But you probably need to delete the objects the map is pointing to. It would probably be a really good idea to use a smart pointer such as Boost shared_ptr in your map instead of native pointers. Then the objects would be cleaned up automatically.

Edit:
Using Boost ptr_map might be an even better idea.

要走干脆点 2024-10-20 02:25:29

内存是“自动释放”的,即整个进程内存被释放,但所指向的对象的析构函数不会被调用。如果您使用 RAII,这可能会导致资源泄漏。

The memory is "automatically freed", in the sense that the entire process memory is freed, but the destructors of the objects pointed to will not be called. This can cause a resource leak, if you use RAII.

柏林苍穹下 2024-10-20 02:25:29

std::map 永远不会对其成员调用 delete 。假设您使用的是相对较新的操作系统,操作系统将在进程终止时回收成员占用的内存,但析构函数将不会运行。

std::map never calls delete on it's members. Assuming you're working with a relatively recent operating system, the OS will reclaim the memory occupied by the members on process termination, but the destructors will not run.

贱贱哒 2024-10-20 02:25:29

如果您有指针映射,那么答案是“否”,您的析构函数将不会被调用,但是“是”,内存将在进程执行结束时被释放。当进程退出(即使崩溃)时,操作系统始终释放进程分配的所有内存,但可能不会调用析构函数。

If you have a map of pointers, then the answer is 'no', your destructors will not be called, but 'yes', the memory will be freed at the end of process execution. All memory allocated by a process is always freed by the Operating System when a process exits (even if it crashes), but destructors might not be called.

瑾夏年华 2024-10-20 02:25:29

内存“泄漏”是指内存在一段时间内无意中未被删除,并且随着进程的继续而最终减少。如果它是一种运行很长时间的进程,例如很少重新启动的服务器,这可能是一个主要问题。

内存泄漏检测器将检测任何已分配但未由编程调用删除的内存,因此 valgrind 等会将其报告为泄漏。

最好使用 valgrind 等程序检查代码,因此“妨碍”越少,就越容易发现真正的泄漏。因此,我的建议是,当您使用 new (或 malloc 或 new[])分配指针时,不要只是让系统为您清理内存或单例等。

您可以通过“清理”程序来执行此操作。只需在映射范围内有一个具有删除器的对象(因为它退出时将被删除),该删除器将清理映射中的指针。由于您需要首先删除对象,因此应晚于地图声明它。

A memory "leak" is where memory is unintentionally not deleted over a period of time, and ends up reducing as the process continues. If it is a type of process that runs for a very long period of time, eg a server that is rarely restarted, this can be a major problem.

Memory leak detectors will pick up on any memory that is allocated and not deleted by a programming call, so valgrind, etc. will report this as a leak.

It is as well to check your code with programs like valgrind, and therefore the less that "gets in the way", the easier it will be to spot real leaks. Therefore my advice is not do just let the system clean up the memory, or singletons, etc, for you when you have allocated a pointer with new (or malloc or new[]).

You can have a "clean-up" routine to do this. Just have an object in the scope of your map that has a deleter (as it will be deleted when it exits) that will clean up the pointers in the map. As you need your object to be deleted first it should be declared later than the map.

高跟鞋的旋律 2024-10-20 02:25:29

就像存储指针的存储类的任何情况一样:您有责任释放它们指向的内存。存储类只负责清理自己的资源。在进程终止时依靠操作系统回收内存是一种不好的做法。

Like in any case of storage class which stores pointers: you are responsible to deallocate memory they point to. Storage class is responsible only to clean its own resources. Relying on reclaiming memory by OS at the process termination is a bad practice.

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