Access violation in std::map

This is a discussion on Access violation in std::map within the C++ Programming forums, part of the General Programming Boards category; I am getting an access violation when tring to access elements in a std::map. The violation occurs on the red ...

  1. #1
    Darkness Prevails Dark_Phoenix's Avatar
    Join Date
    Oct 2006
    Location
    Houston, Texas
    Posts
    174

    Access violation in std::map

    I am getting an access violation when tring to access elements in a std::map. The violation occurs on the red line below.
    Code:
    typedef std::map<wxString, CAccount> mapAccountList;
    class CAccountManager : public wxFrame
    {
        public:
        CAccountManager() { Init(); }
        ~CAccountManager() {  }
        bool SaveAccount(CAccount &Account);
        bool SaveAccountsToDisk();
        bool LoadAccountsFromDisk();
        int GetNumberOfAccounts() { return m_AccountList.size(); }
        const mapAccountList& GetAccountList() const { return m_AccountList; }
     
        private:
        void Init();
        mapAccountList  m_AccountList; 
    };
     
    bool CAccountManager::SaveAccountsToDisk()
    {
        // save all accounts to disk
        if (m_AccountList.empty() )
            return false;
     
        wxFile oFile(ACCOUNT_LIST_FILENAME, wxFile::write);
        if (!oFile.IsOpened() )
        {
            SHOWERROR(wxT("Cannot open output file!") );
            return false;
        }
     
        mapAccountList::iterator iter;
        for (iter = m_AccountList.begin(); iter != m_AccountList.end(); ++iter)
        {
            SAccountInfo AI = iter->second.GetAccountInfo();
    
            if (!oFile.Write(AI.AccountName) )
            {
                SHOWERROR(wxT("Could not write to output file!") );
                oFile.Close();
                return false;
            }
        }
     
        oFile.Close();
        return true;
    }
    I've been beating my brain for an hour now and still can't figure it out. What am I missing here?!?
    Last edited by Dark_Phoenix; 08-16-2009 at 11:20 AM.
    Using Code::Blocks and Windows XP

    In every hero, there COULD be a villain!

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> for (iter = m_AccountList.begin(); iter != m_AccountList.end(); ++iter)

    There's nothing wrong with that line of code - the problem is most likely in the "do some stuff here" part.

  3. #3
    Darkness Prevails Dark_Phoenix's Avatar
    Join Date
    Oct 2006
    Location
    Houston, Texas
    Posts
    174
    Here is the "do some stuff" part (added to OP if that makes it easier..)
    Code:
        {
            SAccountInfo AI = iter->second.GetAccountInfo();
    
            if (!oFile.Write(AI.AccountName) )
            {
                SHOWERROR(wxT("Could not write to output file!") );
                oFile.Close();
                return false;
            }
        }
    Using Code::Blocks and Windows XP

    In every hero, there COULD be a villain!

  4. #4
    Darkness Prevails Dark_Phoenix's Avatar
    Join Date
    Oct 2006
    Location
    Houston, Texas
    Posts
    174
    I hate debugging.... That is the line that the dubbuger is reporting the access violation on. Not sure what it is or how to narrow it down any further than that.....?
    Using Code::Blocks and Windows XP

    In every hero, there COULD be a villain!

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Are you sure the instance of CAccountManager being used is valid at the time of the access violation?

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    First comment out this line:

    >> SAccountInfo AI = iter->second.GetAccountInfo();

    If it runs ok, then change the line to this:

    Code:
    CAccount ca = iter->second;
    If it fails, then look into the CAccount copy constructor, otherwise:

    Code:
    CAccount ca = iter->second;
    ca.GetAccountInfo();
    If that fails, look into CAccount::GetAccountInfo, otherwise:

    Code:
    CAccount ca = iter->second;
    SAccountInfo AI = ca.GetAccountInfo();
    If it fails there, look into the SAccountInfo copy constructor.

    That should narrow it down, at least.

  7. #7
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    I would say the map does not exist. Therefore I would also conclude, without looking at more of your code, that CAccountManager has gone out of scope somehow.

  8. #8
    Darkness Prevails Dark_Phoenix's Avatar
    Join Date
    Oct 2006
    Location
    Houston, Texas
    Posts
    174
    I'm begining to think that Bubba is right. The above code is being called (and the error is occuring) when the program exits. I think the problem is due to the way I am exiting. I am calling the function from inside App::OnExit and I think I should be calling it from inside a wxCloseEvent handler. Not sure if this is right or not but I am going to re write some code and find out for sure.
    Using Code::Blocks and Windows XP

    In every hero, there COULD be a villain!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Access violation... can't figure it out...
    By Raigne in forum C++ Programming
    Replies: 7
    Last Post: 10-11-2007, 11:52 AM
  2. Access violation when reading a string.
    By Desolation in forum C++ Programming
    Replies: 16
    Last Post: 05-01-2007, 11:25 AM
  3. FtpFileFind access violation with MS VC++ 6.0
    By earth_angel in forum C++ Programming
    Replies: 3
    Last Post: 09-22-2005, 08:02 PM
  4. Help! CListCtrl access violation
    By bonkey in forum Windows Programming
    Replies: 4
    Last Post: 11-18-2003, 02:40 PM
  5. 0xC0000005: Access Violation
    By Strider in forum Windows Programming
    Replies: 3
    Last Post: 11-07-2001, 02:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21