单例初始化 [英] singleton initialization

查看:88
本文介绍了单例初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个单例类,如下例所示。应用程序

偶尔在主函数的第二行崩溃,如图所示。

然而,当我更改单例时,静态指针是一个

类成员(在cpp文件中定义)和实例函数

如果指针为NULL则创建对象,那么它工作正常。我会

感谢任何解释为什么会发生这种情况。


class CTestClass

{

公开:

静态CTestClass&实例()

{

静态CTestClass * m_instance =新CTestClass;

返回* m_instance;

}

私人:

CTestClass(){/ *做一些事情* /}

};


int main()

{

CTestClass :: instance(); //在这里初始化单身人士

避免未来的竞争条件

//在使用CTestClass :: instance()时偶尔崩溃在这里

}

-

[见 http://www.gotw.ca/resources/clcm.htm 了解有关的信息]

[comp.lang.c ++。moderated。第一次海报:做到这一点! ]

I created a singleton class as in the example below. The application
sporadically crashes in the second line of the main function as shown.
However, when I change the singleton such that the static pointer is a
class member (defined in the cpp file) and the instance function
creates the object if the pointer is NULL, then it works fine. I would
appreciate any explanations as to why this happens.

class CTestClass
{
public:
static CTestClass& instance()
{
static CTestClass* m_instance = new CTestClass;
return *m_instance;
}
private:
CTestClass() { /* do some stuff */ }
};

int main()
{
CTestClass::instance(); // initialize singleton here to
avoid future race conditions
// sporadically crashes here when using CTestClass::instance()
}
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

推荐答案

Eric写道:
Eric wrote:

我创建了一个单例类如下例所示。应用程序

偶尔在主函数的第二行崩溃,如图所示。

然而,当我更改单例时,静态指针是一个

类成员(在cpp文件中定义)和实例函数

如果指针为NULL则创建对象,那么它工作正常。我会

感谢任何解释为什么会发生这种情况。
I created a singleton class as in the example below. The application
sporadically crashes in the second line of the main function as shown.
However, when I change the singleton such that the static pointer is a
class member (defined in the cpp file) and the instance function
creates the object if the pointer is NULL, then it works fine. I would
appreciate any explanations as to why this happens.



你的代码似乎没有任何不好(除了没有

选择解除分配指针,这样做) 。可能是

在你使用它的上下文中的一些静态变量初始化顺序吗?


问候,


Zeppe

There seems not to be anything bad in your code (except from not having
the choice of deallocating the pointer, doing that way). Could it be
some static variable initialization order in the context in which you
use it?

Regards,

Zeppe


[交叉发布已删除]


5月15日,4:00下午,Eric< shoo ... @ yahoo.comwrote:
[cross-posting deleted]

On May 15, 4:00 pm, Eric <shoo...@yahoo.comwrote:

我创建了一个单例类,如下例所示。应用程序

偶尔在主函数的第二行崩溃,如图所示。

然而,当我更改单例时,静态指针是一个

类成员(在cpp文件中定义)和实例函数

如果指针为NULL则创建对象,那么它工作正常。我会

感谢任何解释为什么会发生这种情况。


class CTestClass

{

公开:

静态CTestClass&实例()

{

静态CTestClass * m_instance =新CTestClass;

返回* m_instance;

}

私人:

CTestClass(){/ *做一些事情* /}


int main()

{

CTestClass :: instance(); //在这里初始化单身人士

避免未来的竞争条件

//使用CTestClass :: instance()偶尔会崩溃这里


}
I created a singleton class as in the example below. The application
sporadically crashes in the second line of the main function as shown.
However, when I change the singleton such that the static pointer is a
class member (defined in the cpp file) and the instance function
creates the object if the pointer is NULL, then it works fine. I would
appreciate any explanations as to why this happens.

class CTestClass
{
public:
static CTestClass& instance()
{
static CTestClass* m_instance = new CTestClass;
return *m_instance;
}
private:
CTestClass() { /* do some stuff */ }

};

int main()
{
CTestClass::instance(); // initialize singleton here to
avoid future race conditions
// sporadically crashes here when using CTestClass::instance()

}



如果运行此代码而只运行此代码,它会偶尔崩溃?如果

那么,你使用什么编译器以及有哪些选项?如果没有,请发布一个完整但最小的样本来证明问题(见常见问题

5.4),因为它可能与单身商业没有直接关系

即使看起来那样。


干杯! --M

If you run this code and only this code, it crashes sporadically? If
so, what compiler are you using and what options? If not, post a
complete but minimal sample that demonstrates the problem (see FAQ
5.4), since it may not be directly related to the singleton business
even if it appears that way.

Cheers! --M


5月15日下午3:36,mlimber< mlim ... @ gmail.comwrote:
On May 15, 3:36 pm, mlimber <mlim...@gmail.comwrote:

如果没有,发布一个

完整但最小的样本来证明问题(参见FAQ

5.4),
If not, post a
complete but minimal sample that demonstrates the problem (see FAQ
5.4),



哎呀。我的意思是FAQ 5.8。


干杯! --M

Oops. I meant FAQ 5.8.

Cheers! --M


这篇关于单例初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆