@@ -57,6 +57,14 @@ Player::~Player()
57
57
}
58
58
}
59
59
60
+ for (const auto & [_, depot] : depotChests) {
61
+ if (Cylinder* parent = depot->getRealParent ()) {
62
+ // remove chest from depot locker, because of possible double free when shared_ptr decides to free up the
63
+ // resource
64
+ parent->internalRemoveThing (depot.get ());
65
+ }
66
+ }
67
+
60
68
if (depotLocker) {
61
69
depotLocker->removeInbox (inbox.get ());
62
70
}
@@ -808,7 +816,7 @@ bool Player::isNearDepotBox() const
808
816
return false ;
809
817
}
810
818
811
- DepotChest* Player::getDepotChest (uint32_t depotId, bool autoCreate)
819
+ DepotChest_ptr Player::getDepotChest (uint32_t depotId, bool autoCreate)
812
820
{
813
821
auto it = depotChests.find (depotId);
814
822
if (it != depotChests.end ()) {
@@ -824,9 +832,10 @@ DepotChest* Player::getDepotChest(uint32_t depotId, bool autoCreate)
824
832
return nullptr ;
825
833
}
826
834
827
- it = depotChests.emplace (depotId, new DepotChest (depotItemId)).first ;
828
- it->second ->setMaxDepotItems (getMaxDepotItems ());
829
- return it->second ;
835
+ const DepotChest_ptr& depotChest =
836
+ depotChests.emplace (depotId, std::make_shared<DepotChest>(depotItemId)).first ->second ;
837
+ depotChest->setMaxDepotItems (getMaxDepotItems ());
838
+ return depotChest;
830
839
}
831
840
832
841
DepotLocker& Player::getDepotLocker ()
@@ -839,8 +848,8 @@ DepotLocker& Player::getDepotLocker()
839
848
DepotChest* depotChest = new DepotChest (ITEM_DEPOT, false );
840
849
// adding in reverse to align them from first to last
841
850
for (int16_t depotId = depotChest->capacity (); depotId >= 0 ; --depotId) {
842
- if (DepotChest* box = getDepotChest (depotId, true )) {
843
- depotChest->internalAddThing (box);
851
+ if (const auto & box = getDepotChest (depotId, true )) {
852
+ depotChest->internalAddThing (box. get () );
844
853
}
845
854
}
846
855
@@ -3199,7 +3208,7 @@ void Player::postRemoveNotification(Thing* thing, const Cylinder* newParent, int
3199
3208
bool isOwner = false ;
3200
3209
3201
3210
for (const auto & it : depotChests) {
3202
- if (it.second == depotChest) {
3211
+ if (it.second . get () == depotChest) {
3203
3212
isOwner = true ;
3204
3213
onSendContainer (container);
3205
3214
}
0 commit comments