Skip to content
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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

ramon-bernardo
Copy link
Contributor

@ramon-bernardo ramon-bernardo commented May 16, 2024

Pull Request Prelude

Changes Proposed

  • Replace party raw pointers with shared_ptr.
  • Use auto and const auto, at all, when possible.

src/events.cpp Show resolved Hide resolved
src/game.cpp Show resolved Hide resolved
@@ -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()) {
Copy link
Contributor

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.

Copy link
Member

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

Copy link
Contributor

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.

@EvilHero90 EvilHero90 added the enhancement Increase or improvement in quality, value, or extent label Jun 2, 2024
@EvilHero90 EvilHero90 added this to the 1.8 milestone Jun 2, 2024
@@ -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()); }
Copy link
Member

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

Copy link
Member

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.

Copy link
Member

@nekiro nekiro Jun 2, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Increase or improvement in quality, value, or extent
Projects
Status: In review
Development

Successfully merging this pull request may close these issues.

None yet

5 participants