Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6715859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:39:53+00:00 2026-05-26T08:39:53+00:00

For some reason when I try and read a property of a pointer to

  • 0

For some reason when I try and read a property of a pointer to an object(GamePlayer) within an std::list (playerlist) it works at first, but when I try to access it later in another function I get a bunch of random numbers instead of the numbers for my client’s socket. That was a mouthful, sorry. I hope someone could shed some light on the situation. I will include a simplified version of the defective code.

class GameRoom  {

    list<GamePlayer*> playerlist;
    locigPort( LogicObj );
}


bool GameRoom::logicPort( LogicObj logit )  {   // This is room[1]

    list<GamePlayer*>::iterator it;

        for (it = playerlist.begin(); it != playerlist.end(); it++){  

                cout << "socket numbers " << (*it)->socketno << endl;  
                /* (*it)->socketno gives me a bunch of random numbers, 
                   not the socket numbers I was looking for! */
        }  

    return true;

}

bool RoomDB::addPlayer( GamePlayer *playerpoint )   {

    roomlist[1].playerlist.push_back( playerpoint );    
    // This adds the player object to the Gameroom object

    cout << "player point " << playerpoint->socketno << " roomno: " << roomno;  
    // This shows everything should be ok so far

    return true;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:39:53+00:00Added an answer on May 26, 2026 at 8:39 am

    The most likely explanation is that you’re calling addPlayer with a pointer than becomes invalid by the time you call logicPort. One possibility is that you call addPlayer with the address of an object on the stack, and the object disappears when the stack is unwound.

    edit The problem is right here:

    bool PlayerDB::addPlayer( int sid, GamePlayer tempplayer ) {
       ...
       roomman.addPlayer( &tempplayer, tempplayer.roomno );
    }
    

    PlayerDB::addPlayer takes the second argument by value. This means that it gets a copy that exists for the lifetime of the method. You then take the pointer to that copy, and add it to the list. Once PlayerDB::addPlayer returns, the pointer becomes invalid.

    It’s hard to suggest a good fix without seeing more code. One possibility is to make PlayerDB::addPlayer take a pointer as its second argument, and make sure you don’t repeat the same mistake one level up the call chain.

    An even better possibility is to turn playerlist into list<GamePlayer>: from your code there doesn’t appear to be any need for the list to contain pointers. This will simplify things greatly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, Section 1 works but Section 2 does not. When run in
For some reason if I try to get the actual size of mystruct I
I'm getting undefined for some reason when I try to return the html via
For some reason every time I try to count the number of rows in
For some reason, when I try to install SQL Server 2008 Express , I
For some reason, when I try to animate textColor , it won't work. The
I wrote this HttpRequest method, but for some reason it always goes to 404
This is probably stupid simple, but for some reason I'm having trouble getting it
I am getting stuck on this problem. It seems simple but for some reason
I am trying to rotate a graphic, but for some reason it won't work.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.