heap corruption problem in singleton class

Discussion in 'C++' started by venkatarao0@gmail.com, Feb 21, 2011.

  1. Guest

    Hi all,

    Here one tricky issue, In my project I have used a singleton class
    used to store data and value info. this information I will use(call
    after system up) 4 times in a year and depends on the requirement not
    frequently. In rare scenario while calling get member function I am
    getting the exception my system is going down(core dump). I
    reproduced the issue bye corrupting the ms_ptr pointer pointing to the
    memory location(in Vxworks 6.7 facility is there) bye manually after
    some time(not immediately) I generated a scenario to call this
    singleton class member function I got the same core dump. This same
    issue I reproduced by ms_ptr pointing to invalid location.

    class config_data
    {
    public:
    typedef config_data this_t;
    typedef map<char*, int> input_data;
    static this_t& instance ();

    void set (const input_data& inData);
    private:
    static this_t* ms_ptr;
    // Constructor
    config_data () {}
    // GET method
    int get (const char *inName, const int inValue);

    // Cannot copy a singleton
    config_data(const this_t&);
    this_t& operator= (const this_t&);
    };

    config_data* config_data::ms_ptr(NULL);
    config_data& config_data::instance ()
    {
    if (!ms_ptr)
    {
    ms_ptr = new config_data;
    }
    return *ms_ptr;
    }
    set & get used to set the values into map and getting the values from
    map. there is iterator problem over here I have not used iterator in
    get also.

    can any one please help me to fix this problem.

    1) I want to know who is corrupting the heap location fist to fix the
    problem. How I come to know while corrupting the heap memory itself
    there is any mechanism to protect/ raise an exception while corrupting
    the memory itself.
     
    , Feb 21, 2011
    #1
    1. Advertising

  2. Guest

    On Feb 21, 10:51 pm, Paavo Helde <> wrote:
    > "" <> wrote innews::
    >
    >
    >
    > > Hi all,

    >
    > > Here one tricky issue, In my project I have used a singleton class
    > > used to store data and value info. this information I will use(call
    > > after system up) 4 times in a year and depends on the requirement not
    > > frequently. In rare scenario while calling get member function I am
    > > getting the exception my system is going down(core dump).  I
    > > reproduced the issue bye corrupting the ms_ptr pointer pointing to the
    > > memory location(in Vxworks 6.7 facility is there) bye manually after
    > > some time(not immediately) I generated a scenario to call this
    > > singleton class member function I got the same core dump.

    >
    > What do you mean? The core dump file was byte-by-byte identical to the
    > first core dump file?


    here the dump is the stack, it will show when and where the exception
    happen this case it is showing the function which is trying to calling
    the singleton member function. the core dump is byte by byte only I
    use the *.elf file used to see the in where this core dump happen.

    >
    > > This same
    > > issue I reproduced by ms_ptr pointing to invalid location.

    >
    > > class config_data
    > > {
    > >   public:
    > >     typedef  config_data         this_t;
    > >     typedef map<char*, int>   input_data;

    >
    > Using 'char*' here seems error-prone, suggesting std::string instead.


    yes here I am using string only I have taken care for NULL termination
    also.

    >
    >
    >
    >
    >
    >
    >
    >
    >
    > >       static this_t&   instance ();

    >
    > >     void   set (const input_data& inData);
    > >   private:
    > >      static this_t*                ms_ptr;
    > >     // Constructor
    > >     config_data ()   {}
    > >     // GET method
    > >  int  get (const char *inName, const int inValue);

    >
    > >     // Cannot copy a singleton
    > >     config_data(const this_t&);
    > >     this_t& operator= (const this_t&);
    > > };

    >
    > > config_data*   config_data::ms_ptr(NULL);
    > > config_data& config_data::instance ()
    > > {
    > >     if (!ms_ptr)
    > >     {
    > >  ms_ptr = new config_data;
    > >     }
    > >     return *ms_ptr;
    > > }
    > > set & get used to set the values into map and getting the values from
    > > map. there is iterator problem over here I have not used iterator in
    > > get also.

    >
    > If yoy think the error appeared in the get() method it would have been
    > wise to include its definition, and maybe also an example of how you call
    > it.

    Initially while device up time this get() member function using there
    we are not facing the problem.

    int get(string)
    {
    int val=0;
    if(input_data.find(string)== input_data.end())
    {
    // insert new string to input_data;
    }
    else
    {
    // val=input_data.second; getting the second value
    }
    return val;
    }
    >
    >
    >
    > >  can any one please help me to fix this problem.

    >
    > > 1) I want to know who is corrupting the heap location fist to fix the
    > > problem. How I come to know while corrupting the heap memory itself
    > > there is any mechanism to protect/ raise an exception while corrupting
    > > the memory itself.

    >
    > Run your code with valgrind, this is a good tool for finding out memory
    > access errors.


    I dont know how to use valgrind. I am running this code in VxWorks 6.7
    RTOS on PowerPC 8560. if u have any more suggestions please reply
    back.

    In VxWorks we can protect the 4k bytes of memory but I cont effort 4k
    instead of 12 bytes.
    >
    > hth
    > Paavo


    Thanx for your reply Paavo.

    I am interested to know how is corrupting the memory & I want to
    raise the exception at the time of memory corruption Then I can give
    instrumentation fix to find the culprit.
     
    , Feb 21, 2011
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    3,813
    Walter
    Apr 29, 2005
  2. Walter Roberson

    My MSVC 6 DLL now has HEAP corruption problems

    Walter Roberson, May 11, 2005, in forum: C Programming
    Replies:
    2
    Views:
    448
    Flash Gordon
    May 12, 2005
  3. Riley DeWiley
    Replies:
    2
    Views:
    783
    Jim Langston
    Jun 27, 2005
  4. Martin the Third
    Replies:
    8
    Views:
    378
    Kai-Uwe Bux
    Jun 13, 2008
  5. finding83

    heap corruption

    finding83, Jul 30, 2009, in forum: C++
    Replies:
    6
    Views:
    594
    James Kanze
    Jul 31, 2009
Loading...

Share This Page