Use virtual destructor for Protobuf Messages.

PiperOrigin-RevId: 560717708
pull/13682/head
Protobuf Team Bot 1 year ago committed by Copybara-Service
parent 78907f8e6c
commit c0d297f767
  1. 4
      src/google/protobuf/repeated_ptr_field.cc
  2. 13
      src/google/protobuf/repeated_ptr_field.h

@ -185,6 +185,10 @@ MessageLite* RepeatedPtrFieldBase::AddWeak(const MessageLite* prototype) {
return result; return result;
} }
void InternalOutOfLineDeleteMessageLite(MessageLite* message) {
delete message;
}
} // namespace internal } // namespace internal
} // namespace protobuf } // namespace protobuf

@ -843,6 +843,8 @@ class PROTOBUF_EXPORT RepeatedPtrFieldBase {
void* tagged_rep_or_elem_; void* tagged_rep_or_elem_;
}; };
void InternalOutOfLineDeleteMessageLite(MessageLite* message);
template <typename GenericType> template <typename GenericType>
class GenericTypeHandler { class GenericTypeHandler {
public: public:
@ -860,9 +862,18 @@ class GenericTypeHandler {
return New(arena); return New(arena);
} }
static inline void Delete(GenericType* value, Arena* arena) { static inline void Delete(GenericType* value, Arena* arena) {
if (arena == nullptr) { if (arena != nullptr) return;
#ifdef __cpp_if_constexpr
if constexpr (std::is_base_of<MessageLite, GenericType>::value) {
// Using virtual destructor to reduce generated code size that would have
// happened otherwise due to inlined `~GenericType`.
InternalOutOfLineDeleteMessageLite(value);
} else {
delete value; delete value;
} }
#else
delete value;
#endif
} }
static inline Arena* GetOwningArena(GenericType* value) { static inline Arena* GetOwningArena(GenericType* value) {
return Arena::InternalGetOwningArena(value); return Arena::InternalGetOwningArena(value);

Loading…
Cancel
Save