STL Map : Uninitialized Memory Read
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: STL Map : Uninitialized Memory Read

  1. #1
    Join Date
    Jan 2006
    Posts
    384

    STL Map : Uninitialized Memory Read

    Hi,

    This is regarding a problem encountered on executing code developed using VS2005 using C++ Standard Libraries.

    I have a member in a class of type std::map. The class definition is indicated below (There are some other class members which I am not indicating here so as to not occupy space)

    Code:
    class DataHolder
    {
    private :
    map<DWORD, MyClass *> m_MyMap;
    
    public :
    DataHolder()
    {
       m_MyMap.clear();
    }
    
    void MyMethod()
    {
            DWORD j = GetValue();
             map<DWORD,MyClass*>::iterator it = m_MyMap.begin();
             it = m_MyMap.find(j); //Rational Purifier indicated a problem in this line
             if (it == m_MyMap.end())
             {
                   printf("Data has not been found in the map");
             }
    
    }
    .
    .
    .
    .
    .
    .
    }
    When the above class is used in the execution of a program and the Rational Purifier tool is being used to analyze memory issues, the following error was indicated in the line marked above :

    "Uninitialized Memory Read "

    The next level of instructions displayed by the Rational Purifier editor shows the following

    Code:
        std::_Tree<class std::_Tmap_traits<unsigned long,class MyClass *,struct std::less<unsigned long>,class std::allocator<struct std::pair<unsigned long const ,class MyClass *> >,0> >::_Lbound(unsigned long const &)const  [c:\program files\microsoft visual studio 8\vc\include\xtree.:1174]
                    _Nodeptr _Wherenode = _Myhead;    // end() if search fails
            
                    while (!_Isnil(_Pnode))
         =>             if (_DEBUG_LT_PRED(this->comp, _Key(_Pnode), _Keyval))
                            _Pnode = _Right(_Pnode);    // descend right subtree
                        else
                            {    // _Pnode not less than _Keyval, remember it
    The problem is indicated in the line shown by "=> " in the snippet above.

    Can someone please provide some clues on why this is being indicated and what would be the possible solution.
    Last edited by humble_learner; February 5th, 2009 at 05:56 AM.

  2. #2
    Join Date
    Nov 2003
    Posts
    1,800

    Re: STL Map : Uninitialized Memory Read

    You've got something else going on. Can you provide a complete compilable example that reproduces the issue?

    gg

  3. #3
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,062

    Re: STL Map : Uninitialized Memory Read

    [ removed duplicate thread ]

  4. #4
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Re: STL Map : Uninitialized Memory Read

    I agree with codeplug. That doesn't mean upload an enormous project file. It means upload a program demonstrating the problem that shows how you are using the std::map and perhaps we will notice something that you are doing incorrectly. At the very least, seeing the complete function or class definition might help. If it is really big, just give us a version that compiles with a simple main function.

    I noticed that you are calling clear() on the map in the constructor. Why? It hasn't even been filled with anything yet. It is already empty by default. That isn't the problem but just something else that I noticed.

  5. #5
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: STL Map : Uninitialized Memory Read

    Hello humble_learner
    I agree with kempofighter. We need to see SSCE...um..something like that.
    The excerpt by itself looks fine. However, calling clear() inside the constructor body seems questionable.
    Quote Originally Posted by humble_learner View Post
    Code:
    DWORD j = GetValue();
    That could be the source of the error, what if somehow j is not being assigned? Then calling
    Code:
    it = m_MyMap.find(j); //Rational Purifier indicated a problem in this line
    will cause trouble. Another possible cause is when you assign the iterator to the empty map like you have done below
    Code:
    map<DWORD,MyClass*>::iterator it = m_MyMap.begin();
    If you recall, T.begin() refers to the first element which in this case has no first element. I'm not sure if that's entirely okay or not though (someone correct me for clarity).

    hope this helps

  6. #6
    Join Date
    Feb 2002
    Posts
    4,640

    Re: STL Map : Uninitialized Memory Read

    Quote Originally Posted by potatoCode View Post
    If you recall, T.begin() refers to the first element which in this case has no first element. I'm not sure if that's entirely okay or not though (someone correct me for clarity).
    That should be fine. If the map is empty, then the iterator returned by 'begin' will be equal to the iterator returned by 'end'.

    Viggy

  7. #7
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: STL Map : Uninitialized Memory Read

    Quote Originally Posted by MrViggy View Post
    That should be fine. If the map is empty, then the iterator returned by 'begin' will be equal to the iterator returned by 'end'.

    Viggy
    Hello MrViggy,
    Aha I see. Thanks for clearing that up for me
    bye~

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center