Jump to content
  • Advertisement
Sign in to follow this  
yewbie

New to classes trying to send a class list pointer to a function :([solved]

This topic is 3496 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

Well I have been messing around with classes for the last few day and im really starting to like them... but im having a bit a trouble and hopefully its something simple I just don't understand about how im allocating my class members or maybe how im passing it to my function. basically what happens is when I try to iterate through my class list I get a access violation with .begin here is my player class
class tPlayer
{
public:
  SOCKET s;							/* socket they connected on */
  int connstate;					/* connection state */
  string playername;				/* player name */

  tStringList outbuf;				/* pending output */
  tStringList inbuf;				/* pending input */
  string address;					/* address player is from */
  int port; 						/* port they connected on */
	
  tPlayer ()	/* constructor */
    {
    s = INVALID_SOCKET;						/* no socket yet */
    connstate = Connection_Connected;	/* Player needs to send login info */
    port = 0;
    };
  
  ~tPlayer ()	/* destructor */
    {
		cout << "Player Disconnected IP:" << address << " port:" << port << endl;
		if (s != INVALID_SOCKET)	/* close connection if active */
			closesocket (s);
    };

	
};
typedef list <tPlayer*> tPlayerList;// we will use an stl list of players
typedef tPlayerList::iterator tPlayerListIterator;//our player iterator
then I declare my player list like so
tPlayerList myplayerlist;				//declare our player object
now I have a function that accepts my playerlist like so
void ProcessClientsIO( tPlayerList * playerlist);
I declare my iterator like so
tPlayerListIterator listiter; //our player list iterator
now when I try to iterate through it if there has been any data added to my playerlist its garbage and crashes on begin
for (listiter = playerlist->begin (); listiter != playerlist->end (); listiter++)
please go easy on me =p, thanks in advance for your replys [Edited by - yewbie on January 24, 2009 5:53:32 PM]

Share this post


Link to post
Share on other sites
Advertisement
std::list<> does not invalidate any iterators when performing insertions. It's one of the reasons that you'd use a list. Iterators are only invalidated when the element that iterator refers to is erased (or when the list is cleared or destroyed).

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
std::list<> does not invalidate any iterators when performing insertions. It's one of the reasons that you'd use a list. Iterators are only invalidated when the element that iterator refers to is erased (or when the list is cleared or destroyed).

Yes, you are right, I realized this immediately after posting, so I deleted the post. Sorry for the confusion.

Share this post


Link to post
Share on other sites
Also, make sure you aren't pulling a NULL pointer out of the list. You weren't very clear on exactly where the error happens, but since you said "if any data has been added" I would say there is a chance you have added bad data, or you are taking it out and using it wrong somehow.

Share this post


Link to post
Share on other sites
Well, thats one of the problems when I run it with no data it iterates perfectly fine, when I add a new player like so

tPlayer * p = new tPlayer; //add a new member to our player list
p->s = s; //set our socket
p->address = inet_ntoa ( sa.sin_addr); //set our ip address
p->port = ntohs (sa.sin_port); //set our port
playerlist.push_back (p); //add player to our linked list of players


then try to iterate through my player list it crashes, my debugger points me to line in the list.h file for the crash point and thats when it start the begin

(sorry again if im not making alot of sense im really new to C++)

could it have something to do with im calling a fucntion with a pointer to my list, then sending that pointer to another function inside the first function?

Share this post


Link to post
Share on other sites
I think this might be the one where its crashing

tPlayerListIterator listiter; //our player list iterator
/* loop through all connections, adding them to the descriptor set */
for (listiter = playerlist.begin (); listiter != playerlist.end (); listiter++)
{
tPlayer * p = *listiter;
if (p->s != INVALID_SOCKET)/* don't bother if connection is closed */
{
if ( (int) p->s > iMaxdesc) //set our max descriptor to the highest socket we have
{
iMaxdesc = p->s;
}

FD_SET( p->s, &in_set );
FD_SET( p->s, &exc_set );

if (!p->outbuf.empty ())/* we are only interested in writing to sockets we have something for */
FD_SET( p->s, &out_set );
} /* end of active player */

} /* end of looping through players */

Share this post


Link to post
Share on other sites
Heh I just spent a hour tracing through it letting it run line by line and found out I was deleting a pointer to my player list (when it was invalid) in a function in a function in a function that was deleting it during iteration, sorry for waisting your time! but I appreciate all your reply you did end up solving my problem =p

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!