Singleton Destructors

后端 未结 12 1711
一整个雨季
一整个雨季 2021-01-31 03:55

Should Singleton objects that don\'t use instance/reference counters be considered memory leaks in C++?

Without a counter that calls for explicit deletion of the singlet

12条回答
  •  不思量自难忘°
    2021-01-31 04:21

    You should explicitly clean up all your objects. Never rely on the OS to clean up for you.

    Where I usually use a singleton is to encapsulate control of something like a file, hardware resource, etc. If I don't properly clean up that connection- I can easily leak system resources. The next time the application runs, it might fail if the resource is still locked by the previous operation. Another issue could be that any finalization- such as writing a buffer to disk- might not occur if it still exists in a buffer owned by a singleton instance.

    This is not a memory leaking issue- the issue is more that you may be leaking resources like other than memory which may not be as easily recovered.

提交回复
热议问题