@@ -778,7 +778,7 @@ static bool THPTensor_(_convertToTensorIndexers)(
778778 // store THPTensors rather than THTensors.
779779
780780 std::vector<Py_ssize_t> indexingDims;
781- std::vector<THPIndexTensor*> indexers;
781+ std::vector<THPPointer< THPIndexTensor>> indexers;
782782
783783 if (THPTensor_ (_checkSingleSequenceTriggersAdvancedIndexing)(index)) {
784784 // Handle the special case where we only have a single indexer
@@ -791,7 +791,7 @@ static bool THPTensor_(_convertToTensorIndexers)(
791791 return false ;
792792 }
793793 indexingDims.push_back (0 );
794- indexers.push_back (indexer);
794+ indexers.push_back (THPPointer<THPIndexTensor>( indexer) );
795795 } else {
796796 // The top-level indexer should be a sequence, per the check above
797797 THPObjectPtr fast (PySequence_Fast (index, NULL ));
@@ -827,14 +827,10 @@ static bool THPTensor_(_convertToTensorIndexers)(
827827 " convertible to LongTensors. The indexing object at position %zd is of type %s "
828828 " and cannot be converted" , i, THPUtils_typename (obj));
829829
830- // Clean up Indexers
831- for (auto & idx : indexers) {
832- Py_DECREF (idx);
833- }
834830 return false ;
835831 }
836832 indexingDims.push_back (i + ellipsisOffset);
837- indexers.push_back (indexer);
833+ indexers.push_back (THPPointer<THPIndexTensor>( indexer) );
838834 }
839835 }
840836 }
@@ -848,7 +844,7 @@ static bool THPTensor_(_convertToTensorIndexers)(
848844 for (const auto & indexer : indexers) {
849845 maybeBroadcasted.emplace_back (THIndexTensor_ (new )(LIBRARY_STATE_NOARGS));
850846 // borrow the underlying Tensor from the indexer map
851- candidates.emplace_back (indexer->cdata );
847+ candidates.emplace_back (indexer. get () ->cdata );
852848 }
853849
854850 // Broadcast/Expand indexing Tensors as necessary
@@ -887,10 +883,6 @@ static bool THPTensor_(_convertToTensorIndexers)(
887883 " for dimension %lld (of size %lld)" ,
888884 (long long )indexAtDim, (long long )dim, (long long )sizeAtDim);
889885
890- // Clean up Indexers
891- for (auto & idx : indexers) {
892- Py_DECREF (idx);
893- }
894886
895887 return false ;
896888 }
@@ -905,17 +897,9 @@ static bool THPTensor_(_convertToTensorIndexers)(
905897 }
906898 PyErr_Format (PyExc_IndexError, " The advanced indexing objects could not be broadcast" );
907899
908- // Clean up Indexers
909- for (auto & idx : indexers) {
910- Py_DECREF (idx);
911- }
912900 return false ;
913901 }
914902
915- // Clean up Indexers
916- for (auto & idx : indexers) {
917- Py_DECREF (idx);
918- }
919903 return true ;
920904}
921905
0 commit comments