-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use Party shared_ptr instead of raw pointers #4687
base: master
Are you sure you want to change the base?
Conversation
@@ -564,8 +563,7 @@ ChatChannel* Chat::getChannel(const Player& player, uint16_t channelId) | |||
} | |||
|
|||
case CHANNEL_PARTY: { | |||
Party* party = player.getParty(); | |||
if (party) { | |||
if (const auto& party = player.getParty()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's been my experience that declaring a variable and checking it at same time inside the if statement causes crashes. I'm sure this isn't true in all cases, but I'm just stating what has been my experience so far.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How so? It's equivalent other than the scoping (which would cause compilation errors, not crashes)
Would be interesting to see a form of reproduction though
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have an example you can use to reproduce. I will contact you about it on discord.
@@ -12,11 +12,11 @@ | |||
extern Game g_game; | |||
extern Events* g_events; | |||
|
|||
Party::Party(Player* leader) : leader(leader) { leader->setParty(this); } | |||
Party::Party(Player* leader) : leader(leader) { leader->setParty(shared_from_this()); } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shared_from_this() is not available in constructor, so this will throw exception
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, arguably setParty
is responsibility of the function that creates the party.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, there should be clear ownership which is now unknown, we could say its the leader who owns a party, but this may lead to crashes if leader is deleted ex on logout (this happens on disband with no members in party, look how leader party is set to nullptr which triggers delete on party pointer, which is later used). Imo this should follow the pattern and store every created party in game class, party class should have static factory method such as createParty (similar to createNpc) and members and leader should have a weak reference to the party.
Pull Request Prelude
Changes Proposed
auto
andconst auto
, at all, when possible.