CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 2003
    Posts
    2,185

    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!

  2. #2
    Join Date
    Nov 2003
    Posts
    2,185

    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?

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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&#37; 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.

  4. #4
    Join Date
    Nov 2003
    Posts
    2,185

    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 ***.

  5. #5
    Join Date
    Apr 2008
    Posts
    725

    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

  6. #6
    Join Date
    Nov 2003
    Posts
    2,185

    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
  •  





Click Here to Expand Forum to Full Width

Featured