-
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!
-
April 1st, 2010, 08:19 AM
#2
Re: auto_ptr returns garbage
Additional info: I have changed all calls to CSettings::Instance() instead of storing the pointer.
Now the first pointer I initialize gets the address 0x00000007, and that seems to be the issue (because the singleton class was the one getting that address before).
Any ideas?
-
April 1st, 2010, 09:43 AM
#3
Re: auto_ptr returns garbage
I'm not really a fan of auto_ptr. It's hard to use correctly. While it seems like this usage *should* be fine, it might be easier to just avoid it. Stick with shared_ptr or scoped_ptr (or, if you have a bleeding-edge compiler, unique_ptr) for all your smart pointer needs.
The following is not 100% thread-safe on all compilers, but then, your above code isn't either, so I don't consider it a problem.....you can make either one thread-safe by ensuring you call Instance() at least once before spawning any threads.
Code:
CSettings * CSettings::Instance()
{
static CSettings instance;
return &instance;
}
Last edited by Lindley; April 1st, 2010 at 09:46 AM.
-
April 2nd, 2010, 04:52 AM
#4
Re: auto_ptr returns garbage
Thanks for your reply.
I have been studying this issue for some time, and I think it has something to do with a corrupted heap. Currently trying to figure out where this is coming from, but it's a pain in the ***.
-
April 2nd, 2010, 09:47 AM
#5
Re: auto_ptr returns garbage
it looks like your static intialisation doesnt set it to NULL, but i'm not overly familiar with auto_ptr
-
April 2nd, 2010, 09:50 AM
#6
Re: auto_ptr returns garbage
In the end, it had nothing to do with auto_ptr (it was just the first pointer initialized to get corrupted). It was a corrupted heap due to an external component I used. I have replaced the component and all seems to work fine now.
Thank you all for looking into my problem!
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
|