Jump to content
  • Advertisement
Sign in to follow this  
Smit

std::list access violation

This topic is 4453 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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
}

Share this post


Link to post
Share on other sites
It seems valid ( 0xcdcdcd ) right before I call AddMessage( ).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!