std::list access violation

Started by
7 comments, last by Smit 17 years, 11 months ago
I have the following classes:

class CMessage
{
   int i;
   float p1;
   float p2;
}


class CMessageHandler
{
public:

  void AddMessage( CMessage msg ) {
    m_list.push_back( msg );
  }


private:

  std::list< CMessage > m_list;
};
And calling AddMessage() causes an access violation, and I have no idea why. Could someone explain it for me please? Thanks.
Advertisement
Smit,

How is your CMessageHandler object being created? Its possible the access violations isnt because of the std::list but because the Message handler object itself in uninitialized or invalid.

Cheers!
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
It's created as CMessageHandler* m_Handler = new CMessageHandler( );

I thought AddMessage wouldn't be able to be called if it isn't valid at that time. I stepped through the code, and all of the pointers are valid.

CRenderWindow contains a pointer to CKernel. It uses this to call CKernel::PostMessage( Params ); CKernel has it's own pointer to CMessageHandler and trys to call CMessageHandler::AddMessage( CMessage msg ); which is where the av happens.
Quote:Original post by Smit
It's created as CMessageHandler* m_Handler = new CMessageHandler( );

I thought AddMessage wouldn't be able to be called if it isn't valid at that time.


It can't be, and that's why your compiler doesn't like it. I might suggest that m_Handler->AddMessage() is being called before it is set to new CMessageHandler();

Can you post some more code to help us figure it out with you?
XBox 360 gamertag: templewulf feel free to add me!
Well, you can still call it, it would just be an "access violation." Have you tried stepping through the code and checking whether the CMessageHandler is a valid pointer at the time when you call its member functions?

Also, in the CMessageHandler constructor you might initialize the list in the initializer list. This shouldn't be necessary, but couldnt hurt. Example:

CMessageHandler::CMessageHandler() : m_list(){    // Rest of initialization code}
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
It seems valid ( 0xcdcdcd ) right before I call AddMessage( ).
Quote:Original post by Smit
It seems valid ( 0xcdcdcd ) right before I call AddMessage( ).


That value (repeating values are always suspect) are usually set while in a debug mode to show that it is uninitialised.

So you are calling the function on a bad pointer.
That the value some compilers choose to set uninitialized values to when you don't do it explicitely. Predefined values like that for certain situations helps debugging, since if you encounter them, you know what's wrong (uninitialized memory, freed memory, array bounds for example). So in short, that memory adress is anything BUT valid.
Quote:Original post by Brother Bob
That the value some compilers choose to set uninitialized values to when you don't do it explicitely. Predefined values like that for certain situations helps debugging, since if you encounter them, you know what's wrong (uninitialized memory, freed memory, array bounds for example). So in short, that memory adress is anything BUT valid.


I thought that might be the case.

This topic is closed to new replies.

Advertisement