|
-
April 1st, 2010, 07:54 AM
#1
auto_ptr returns garbage
I have a singleton settings class which is initialized and contains the right data. Then, in another class (UI), I want to store the pointer to the settings class so I don't have to retrieve the pointer every time again I want to use the settings class.
In the constructor, I have this code:
CSettings * pSettings = CSettings::Instance();
The instance function looks like this:
Code:
CSettings * CSettings::Instance()
{
if (sm_inst.get() == NULL) sm_inst.reset(new CSettings);
return sm_inst.get();
}
sm_inst is declared as this:
static std::auto_ptr<CSettings> sm_inst;
and initialized as this:
std::auto_ptr<CSettings> CSettings::sm_inst;
When I check the value of pSettings after the assignment, it has a valid pointer, but all the values are garbage (strings are bad pointers, etc). However, when I step through the code, it does get the right object. Somehow, it seems that the sm_inst.get() is too slow or something like that.
When I take a look at the object using QuickWatch, the object itself is scrambled, but the sm_inst variable does contain a valid copy with all the correct values.
Now I have 2 questions:
1) Is it a good practice to store the pointer to the single class, or should I call CSettings::Instance() every time?
2) Why does the singleton class does not return a valid object immediately?
Thanks in advance!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|