From b5ea70aa4432786a6d26fd00d5695ed7f4306d32 Mon Sep 17 00:00:00 2001 From: "Marco Antonio J. Costa" Date: Wed, 22 Jul 2026 11:10:40 -0300 Subject: [PATCH] declare the postal service iterators as values, not as references PostalService.h names every type twice, once plainly and once as a reference: RefToDestinationStruct is DestinationStruct&, RefToShipmentList is ShipmentList&. That scheme was extended to the iterators, and there it does not work. Every one of the seventeen uses looks like this: RefToShipmentListIterator sli = _Shipments.begin(); begin() returns a value, so this binds a non-const reference to a temporary. MSVC allows it as an extension; the temporary still dies at the end of the full expression, which leaves sli referring to a dead object for the whole rest of the function. It works only because nothing has reused the stack slot yet. clang rejects it outright. Iterators are already handles into their container, so a reference to one buys nothing: the plain typedef says exactly what these seventeen variables want to be. DESTINATION and SHIPMENT now take theirs by value, which changes nothing for either, as both only dereference the iterator they are given. Two of the reference typedefs, RefToDestinationDeliveryInfoTableIterator and RefToDeliveryCallbackDataListIterator, had no uses at all. They are removed rather than left behind, since their only possible use is the pattern this commit removes. Verification: grep -rn 'RefTo[A-Za-z]*Iterator' --include=*.cpp --include=*.h . # nothing ninja -C build parse # bind-to-temporary class gone: 91 -> 60 sites ninja -C build -k 0 # Release, four applications, green ninja -C build-debug -k 0 # Debug, four applications, green Co-Authored-By: Claude Opus 4.8 --- Laptop/BobbyR.cpp | 4 ++-- Laptop/BobbyRShipments.cpp | 6 +++--- Laptop/PostalService.cpp | 20 ++++++++++---------- Laptop/PostalService.h | 11 ++++------- Laptop/XML_DeliveryMethods.cpp | 3 +-- Laptop/XML_ShippingDestinations.cpp | 2 +- Laptop/florist Order Form.cpp | 2 +- 7 files changed, 22 insertions(+), 26 deletions(-) diff --git a/Laptop/BobbyR.cpp b/Laptop/BobbyR.cpp index 037295cb8..747882823 100644 --- a/Laptop/BobbyR.cpp +++ b/Laptop/BobbyR.cpp @@ -219,7 +219,7 @@ BOOLEAN EnterBobbyR() gShipmentTable.erase(gShipmentTable.begin(), gShipmentTable.end()); } - RefToDestinationListIterator dli = gPostalService.LookupDestinationList().begin(); + DestinationListIterator dli = gPostalService.LookupDestinationList().begin(); while (dli != gPostalService.LookupDestinationList().end()) { @@ -228,7 +228,7 @@ BOOLEAN EnterBobbyR() } - RefToShipmentListIterator sli = gPostalService.LookupShipmentList().begin(); + ShipmentListIterator sli = gPostalService.LookupShipmentList().begin(); while (sli != gPostalService.LookupShipmentList().end()) { diff --git a/Laptop/BobbyRShipments.cpp b/Laptop/BobbyRShipments.cpp index dc8db902e..4e76bcf2d 100644 --- a/Laptop/BobbyRShipments.cpp +++ b/Laptop/BobbyRShipments.cpp @@ -191,7 +191,7 @@ BOOLEAN EnterBobbyRShipments() INT32 iCnt=0; //get the first shipment # - vector::iterator& psi = gShipmentTable.begin(); + vector::iterator psi = gShipmentTable.begin(); while(psi != gShipmentTable.end()) { @@ -299,7 +299,7 @@ void RenderBobbyRShipments() if(giBobbyRShipmentSelectedShipment != -1) { - RefToShipmentPackageListIterator spli = gShipmentTable[giBobbyRShipmentSelectedShipment]->ShipmentPackages.begin(); + ShipmentPackageListIterator spli = gShipmentTable[giBobbyRShipmentSelectedShipment]->ShipmentPackages.begin(); int j; for(unsigned i = 0; i < gShipmentTable[giBobbyRShipmentSelectedShipment]->ShipmentPackages.size(); i++, spli++) { @@ -452,7 +452,7 @@ void DisplayPreviousShipments() UINT32 uiItemCnt; UINT8 ubFontColor = BOBBYR_SHIPMENT_STATIC_TEXT_COLOR; - RefToShipmentListIterator sli = gPostalService.LookupShipmentList().begin(); + ShipmentListIterator sli = gPostalService.LookupShipmentList().begin(); uiNumItems = (UINT32)gPostalService.LookupShipmentList().size(); if(uiNumItems > BOBBYR_SHIPMENT_NUM_PREVIOUS_SHIPMENTS) diff --git a/Laptop/PostalService.cpp b/Laptop/PostalService.cpp index ff2bdfeb8..359748a51 100644 --- a/Laptop/PostalService.cpp +++ b/Laptop/PostalService.cpp @@ -124,7 +124,7 @@ UINT16 CPostalService::CreateNewShipment(UINT16 usDestinationID, UINT8 ubDelive } _Destinations.sort(DESTINATION_LIST_ASCENDING); - RefToDestinationListIterator dli = _Destinations.begin(); + DestinationListIterator dli = _Destinations.begin(); while(DESTINATION(dli).usID != usDestinationID) { @@ -186,7 +186,7 @@ BOOLEAN CPostalService::AddPackageToShipment(UINT16 usShipmentID, UINT16 usItemI } _Shipments.sort(SHIPMENT_LIST_ASCENDING); - RefToShipmentListIterator sli = _Shipments.begin(); + ShipmentListIterator sli = _Shipments.begin(); while(SHIPMENT(sli).usID != usShipmentID) { @@ -218,7 +218,7 @@ BOOLEAN CPostalService::SendShipment(UINT16 usShipmentID) return FALSE; } - RefToShipmentListIterator sli = _Shipments.begin(); + ShipmentListIterator sli = _Shipments.begin(); while(SHIPMENT(sli).usID != usShipmentID) { @@ -272,7 +272,7 @@ BOOLEAN CPostalService::DeliverShipment(UINT16 usShipmentID) return FALSE; } - RefToShipmentListIterator sli = _Shipments.begin(); + ShipmentListIterator sli = _Shipments.begin(); while(SHIPMENT(sli).usID != usShipmentID) { @@ -622,7 +622,7 @@ BOOLEAN CPostalService::DeliverShipmentForMultiplayer(UINT16 usShipmentID) return FALSE; } - RefToShipmentListIterator sli = _Shipments.begin(); + ShipmentListIterator sli = _Shipments.begin(); while(SHIPMENT(sli).usID != usShipmentID) { @@ -897,7 +897,7 @@ BOOLEAN CPostalService::SaveShipmentListToSaveGameFile(HWFILE hFile) return TRUE; } - RefToShipmentListIterator sli = _Shipments.begin(); + ShipmentListIterator sli = _Shipments.begin(); ShipmentSaveFileDataStruct sfs; ShipmentPackageStruct sps; @@ -1022,7 +1022,7 @@ BOOLEAN CPostalService::IsSectorAShipmentSector(UINT8 ubMapX, UINT8 ubMapY, UINT BOOLEAN isShipmentSector = FALSE; vector destinations; - RefToDestinationListIterator dli = LookupDestinationList().begin(); + DestinationListIterator dli = LookupDestinationList().begin(); while (dli != LookupDestinationList().end()) { @@ -1055,7 +1055,7 @@ UINT16 CPostalService::GetShipmentCount(SHIPMENT_STATUS TargetedShipmentStatus) return 0; } - RefToShipmentListIterator sli = _Shipments.begin(); + ShipmentListIterator sli = _Shipments.begin(); UINT16 usCnt=0; while(sli != _Shipments.end()) @@ -1119,7 +1119,7 @@ UINT16 CPostalService::SetDestinationDeliveryInfo(UINT8 ubDeliveryMethodIndex, U _Destinations.sort(DESTINATION_LIST_ASCENDING); - RefToDestinationListIterator dli = _Destinations.begin(); + DestinationListIterator dli = _Destinations.begin(); while( DESTINATION(dli).uiIndex != uiDestinationIndex) { @@ -1160,7 +1160,7 @@ RefToDestinationStruct CPostalService::_GetDestination(UINT16 usDestinationID) return DESTINATION(_Destinations.end()); } - RefToDestinationListIterator dli = _Destinations.begin(); + DestinationListIterator dli = _Destinations.begin(); while(DESTINATION(dli).usID != usDestinationID) { diff --git a/Laptop/PostalService.h b/Laptop/PostalService.h index 3830a973e..cf6e96b76 100644 --- a/Laptop/PostalService.h +++ b/Laptop/PostalService.h @@ -45,8 +45,8 @@ typedef DestinationStruct* PDestinationStruct; typedef list DestinationList; typedef list& RefToDestinationList; -typedef DestinationList::const_iterator& RefToDestinationListIterator; -inline RefToDestinationStruct DESTINATION(RefToDestinationListIterator dli) +typedef DestinationList::const_iterator DestinationListIterator; +inline RefToDestinationStruct DESTINATION(DestinationListIterator dli) { return ( (RefToDestinationStruct) *dli ); } @@ -81,7 +81,6 @@ typedef vector DestinationDeliveryInfoTable; typedef DestinationDeliveryInfoTable* PDestinationDeliveryInfoTable; typedef DestinationDeliveryInfoTable& RefToDestinationDeliveryInfoTable; typedef DestinationDeliveryInfoTable::iterator DestinationDeliveryInfoTableIterator; -typedef DestinationDeliveryInfoTableIterator& RefToDestinationDeliveryInfoTableIterator; typedef struct { @@ -104,7 +103,7 @@ typedef ShipmentPackageStruct& RefToShipmentPackageStruct; typedef ShipmentPackageStruct* PShipmentPackageStruct; typedef vector ShipmentPackageList; typedef ShipmentPackageList& RefToShipmentPackageList; -typedef ShipmentPackageList::iterator& RefToShipmentPackageListIterator; +typedef ShipmentPackageList::iterator ShipmentPackageListIterator; typedef enum { @@ -130,7 +129,6 @@ typedef list ShipmentList; typedef ShipmentList* PShipmentList; typedef ShipmentList& RefToShipmentList; typedef ShipmentList::iterator ShipmentListIterator; -typedef ShipmentListIterator& RefToShipmentListIterator; typedef struct { @@ -159,7 +157,7 @@ inline BOOLEAN SHIPMENT_ID_LOWER(RefToShipmentStruct s1, RefToShipmentStruct s2) } #define SHIPMENT_LIST_ASCENDING SHIPMENT_ID_LOWER -inline RefToShipmentStruct SHIPMENT(RefToShipmentListIterator sli) +inline RefToShipmentStruct SHIPMENT(ShipmentListIterator sli) { return ( (RefToShipmentStruct) *sli ); } @@ -192,7 +190,6 @@ typedef DeliveryCallbackDataStruct& RefToDeliveryCallbackDataStruct; typedef vector DeliveryCallbackDataList; typedef DeliveryCallbackDataList& RefToDeliveryCallbackDataList; typedef DeliveryCallbackDataList::iterator DeliveryCallbackDataListIterator; -typedef DeliveryCallbackDataListIterator& RefToDeliveryCallbackDataListIterator; //////////////////////////////////////////////////////////////// class CPostalService diff --git a/Laptop/XML_DeliveryMethods.cpp b/Laptop/XML_DeliveryMethods.cpp index 652cea6bf..4ab39a69f 100644 --- a/Laptop/XML_DeliveryMethods.cpp +++ b/Laptop/XML_DeliveryMethods.cpp @@ -23,7 +23,6 @@ typedef DestinationDeliveryInfoReadInStruct& RefToDestinationDeliveryInfoReadInS typedef vector DestinationDeliveryInfoReadInTable; typedef DestinationDeliveryInfoReadInTable& RefToDestinationDeliveryInfoReadInTable; typedef DestinationDeliveryInfoReadInTable::iterator DestinationDeliveryInfoReadInTableIterator; -typedef DestinationDeliveryInfoReadInTableIterator& RefToDestinationDeliveryInfoReadInTableIterator; enum { @@ -145,7 +144,7 @@ deliveryMethodEndElementHandle(void *userData, const XML_Char *name) pData->curElement = DELIVERYMETHOD_ELEMENT_TABLE; UINT8 ubCurrentDM = gPostalService.AddDeliveryMethod(pData->CurDeliveryMethod.szDescription); - RefToDestinationDeliveryInfoReadInTableIterator dfriti = pData->CurDeliveryMethod.DestinationDeliveryInfos->begin(); + DestinationDeliveryInfoReadInTableIterator dfriti = pData->CurDeliveryMethod.DestinationDeliveryInfos->begin(); while(dfriti != pData->CurDeliveryMethod.DestinationDeliveryInfos->end()) { diff --git a/Laptop/XML_ShippingDestinations.cpp b/Laptop/XML_ShippingDestinations.cpp index 01ea32067..651bcae23 100644 --- a/Laptop/XML_ShippingDestinations.cpp +++ b/Laptop/XML_ShippingDestinations.cpp @@ -118,7 +118,7 @@ destinationEndElementHandle(void *userData, const XML_Char *name) // using uiIndex, iterate thru gPostalService::_Destinations till we find the correct // DestinationStruct, and change its wstrName - RefToDestinationListIterator dli = gPostalService.LookupDestinationList().begin(); + DestinationListIterator dli = gPostalService.LookupDestinationList().begin(); while(DESTINATION(dli).uiIndex != pData->tempDest.uiIndex) { diff --git a/Laptop/florist Order Form.cpp b/Laptop/florist Order Form.cpp index 1bba206ab..281f46523 100644 --- a/Laptop/florist Order Form.cpp +++ b/Laptop/florist Order Form.cpp @@ -1347,7 +1347,7 @@ void InitFloristOrderForm() gDestinationTable.erase(gDestinationTable.begin(), gDestinationTable.end()); } - RefToDestinationListIterator dli = gPostalService.LookupDestinationList().begin(); + DestinationListIterator dli = gPostalService.LookupDestinationList().begin(); while (dli != gPostalService.LookupDestinationList().end()) {