关于singletone的疑问,急急急

Hinagi 2009-10-29 08:53:40
class Singleton {
 public: static Singleton* Instance();
 protected: Singleton();
 private: static Singleton* _instance;
}
// Implementation
Singleton* Singleton::_instance = 0;
Singleton* Singleton::Instance() {
 if (_instance == 0) {
   _instance = new Singleton;
 }
return _instance;
}


在单态模式中为什么我只看到new, 没有delete啊,我应该在什么时候delete掉这个对象??
在析构函数里面肯定不行,我试过了,因为要delete以后才能进析构函数,我的程序现在有内存泄露,就是这个问题,怎么解决啊?
...全文
129 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
Hinagi 2009-10-29
  • 打赏
  • 举报
回复
好了,你那个是写在一个文件里面的,我把instance的声明和实现分开的,要在cpp头加一个static的声明才可以
Hinagi 2009-10-29
  • 打赏
  • 举报
回复
包含了以后有一个连接错误

>AudioCapture.obj : error LNK2001: unresolved external symbol "public: static class std::auto_ptr<class CAudioCapture> CAudioCapture::m_pInstance" (?m_pInstance@CAudioCapture@@2V?$auto_ptr@VCAudioCapture@@@std@@A)
老邓 2009-10-29
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 kenshintang1215 的回复:]
引用 12 楼 loaden 的回复:
不需要你来初始化。
这个类一实例后,初始化工作自己完成。
用我给的例子改写你的单件模式,去掉外部的delete,就可以正常工作了。


编译报错诶,就上面的错误

另:
我建的是Windows mobile上的unicode的工程
[/Quote]
需要包含头文件:
#include <memory>

我在14楼回复了。
Hinagi 2009-10-29
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 loaden 的回复:]
不需要你来初始化。
这个类一实例后,初始化工作自己完成。
用我给的例子改写你的单件模式,去掉外部的delete,就可以正常工作了。
[/Quote]

编译报错诶,就上面的错误

另:
我建的是Windows mobile上的unicode的工程
老邓 2009-10-29
  • 打赏
  • 举报
回复
需要包含头文件:
#include <memory>
thy38 2009-10-29
  • 打赏
  • 举报
回复
[Quote=引用楼主 kenshintang1215 的回复:]
在析构函数里面肯定不行,我试过了,因为要delete以后才能进析构函数,我的程序现在有内存泄露,就是这个问题,怎么解决啊?
[/Quote]
出了作用域不就自动调用析构嘛
老邓 2009-10-29
  • 打赏
  • 举报
回复
不需要你来初始化。
这个类一实例后,初始化工作自己完成。
用我给的例子改写你的单件模式,去掉外部的delete,就可以正常工作了。
Hinagi 2009-10-29
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 loaden 的回复:]
引用 6 楼 kenshintang1215 的回复:
引用 4 楼 loaden 的回复:
核心例子代码:C/C++ codestd::auto_ptr <Cmd> Cmd::m_inst;

Cmd& Cmd::GetInstance()
{if (m_inst.get()== NULL) m_inst.reset(new Cmd());return*m_inst;
}


没看懂诶

比如:
C/C++ codeclass Singleton
{static std::auto_ptr<Singleton> m_pInstance;protected://prevent user making our any instance by manually Singleton()
{}public:~Singleton()
{}//Return this singleton class' instance pointerstatic Singleton* Instance()
{if(!m_pInstance.get())
{
m_pInstance= std::auto_ptr<Singleton>(new Singleton());
}return m_pInstance.get();
}
};
[/Quote]


这个智能指针需要初始化不? static的对象诶,怎么初始化?

另:
1>.\AudioCapture.cpp(128) : error C2065: 'm_pInstance' : undeclared identifier
1>.\AudioCapture.cpp(128) : error C2228: left of '.get' must have class/struct/union
1> type is ''unknown-type''
1>.\AudioCapture.cpp(130) : error C2653: 'std' : is not a class or namespace name
1>.\AudioCapture.cpp(130) : error C2275: 'CAudioCapture' : illegal use of this type as an expression
1>.\AudioCapture.cpp(132) : error C2228: left of '.get' must have class/struct/union
1> type is ''unknown-type''
1>.\AudioCapture.cpp(166) : error C2541: 'delete' : cannot delete objects that are not pointers
老邓 2009-10-29
  • 打赏
  • 举报
回复
delete CAudioCapture::GetInstance();
delete CAudioPlayer::GetInstance();

不要外部delete!
不能这样写的。
老邓 2009-10-29
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 kenshintang1215 的回复:]
引用 4 楼 loaden 的回复:
核心例子代码:C/C++ codestd::auto_ptr <Cmd> Cmd::m_inst;

Cmd& Cmd::GetInstance()
{if (m_inst.get()== NULL) m_inst.reset(new Cmd());return*m_inst;
}


没看懂诶
[/Quote]
比如:
    class Singleton
{
static std::auto_ptr<Singleton> m_pInstance;
protected:
//prevent user making our any instance by manually
Singleton()
{}
public:
~Singleton()
{}
//Return this singleton class' instance pointer
static Singleton* Instance()
{
if(!m_pInstance.get())
{
m_pInstance = std::auto_ptr<Singleton>(new Singleton());
}
return m_pInstance.get();
}
};
Hinagi 2009-10-29
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 lingyin55 的回复:]
关于C++单件模式释放对象
http://blog.csdn.net/windboyzsj/archive/2008/08/18/2790485.aspx
[/Quote]

看了,不过因为我的是一个windows窗口函数(只有一个对话框),我把delete CA::getInstanse()放在消息循环里面,也就是放在点了关闭按钮那里,不知道为什么,析构函数一直被循环调用.....程序结束不了了
...
else if (IDCANCEL == LOWORD(wParam))
{
delete CAudioCapture::GetInstance();
delete CAudioPlayer::GetInstance();

EndDialog(hWnd, LOWORD(wParam));
PostQuitMessage(0);
return 0;
}
...
lingyin55 2009-10-29
  • 打赏
  • 举报
回复
http://www.cnetnews.com.cn/2008/0415/815018.shtml
http://blog.csdn.net/forgetmiss/archive/2009/04/12/4066729.aspx

自己看看吧
Hinagi 2009-10-29
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 loaden 的回复:]
核心例子代码:C/C++ codestd::auto_ptr<Cmd> Cmd::m_inst;

Cmd& Cmd::GetInstance()
{if (m_inst.get()== NULL) m_inst.reset(new Cmd());return*m_inst;
}
[/Quote]

没看懂诶
pcboyxhy 2009-10-29
  • 打赏
  • 举报
回复
删除要你自己解决,在你确定不需要使用这个实例之后,可以删除
老邓 2009-10-29
  • 打赏
  • 举报
回复
核心例子代码:
std::auto_ptr<Cmd> Cmd::m_inst;

Cmd& Cmd::GetInstance()
{
if (m_inst.get() == NULL) m_inst.reset(new Cmd());
return *m_inst;
}
老邓 2009-10-29
  • 打赏
  • 举报
回复
这确实会内存泄露的。
你可以使用智能指针:auto_ptr
healer_kx 2009-10-29
  • 打赏
  • 举报
回复
是Singleton。
lingyin55 2009-10-29
  • 打赏
  • 举报
回复
关于C++单件模式释放对象
http://blog.csdn.net/windboyzsj/archive/2008/08/18/2790485.aspx

64,318

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧