- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
所以,经典的简单单例实现如下:
class Singleton
{
private:
static Singleton* singleton;
Singleton() {}
public:
static Singleton* getInstance();
};
cpp 文件:
Singleton* Singleton::singleton = 0;
Singleton* Singleton::getInstance()
{
if (!singleton)
{
singleton = new Singleton;
}
return singleton;
}
我在这里看到内存泄漏 - '因为 new 没有 delete。但是在 C++ 中没有静态析构函数,所以我们不关心这个内存泄漏?
最佳答案
内存泄漏不仅仅是没有匹配空闲的分配。当您拥有可以回收的内存时,因为该对象不再使用,但实际上并没有被释放。事实上,许多内存泄漏是程序中有代码释放内存的情况,但无论出于何种原因,它都没有被调用(例如,引用循环)。有很多关于如何检测这些泄漏的研究。 this paper 是此类工具的一个很好的例子。
在单例的情况下,我们没有泄漏,因为该单例存在于整个程序中。它的生命周期永远不会结束,因此没有被回收的内存不是问题。
也就是说,您上面的代码并不是大多数人会实现单例的方式。规范的 C++ 实现是这样的:
class Singleton
{
private:
/* No instantiation. */
Singleton() {}
/* Explicitly disallow copying. */
Singleton(const Singleton&) = delete;
Singleton& operator= (const Singleton&) = delete;
/* In C++03, the above would be written as
*
* Singleton(const Singleton&);
* Singleton& operator= (const Singleton&);
*
* and you'd just leave the methods unimplemented.
*/
public:
static Singleton& getInstance();
};
.cpp 文件:
Singleton& Singleton::getInstance() {
/* Have a static local variable representing the unique instance. Since
* it's static, there is only one instance of this variable. It's also only
* initialized when getInstance is called.
*/
static Singleton theInstance;
return theInstance;
}
现在根本没有动态分配 - 内存由编译器分配,并且可能驻留在代码或数据段中,而不是在堆中。另请注意,您必须明确禁止复制,否则您可能会得到许多单例的克隆。
这样做的另一个优点是 C++ 保证在程序退出时(假设程序正常终止),theInstance
的析构函数确实会在程序结束时触发。因此,您可以使用所需的所有清理代码定义析构函数。
希望这会有所帮助!
关于c++ - 静态字段的析构函数。单例实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9968162/
我最近购买了《C 编程语言》并尝试了 Ex 1-8这是代码 #include #include #include /* * */ int main() { int nl,nt,nb;
早上好!我有一个变量“var”,可能为 0。我检查该变量是否为空,如果不是,我将该变量保存在 php session 中,然后调用另一个页面。在这个新页面中,我检查我创建的 session 是否为空,
我正在努力完成 Learn Python the Hard Way ex.25,但我无法理解某些事情。这是脚本: def break_words(stuff): """this functio
我是一名优秀的程序员,十分优秀!