diff options
author | bulatman <bulatman@yandex-team.com> | 2023-05-23 11:35:30 +0300 |
---|---|---|
committer | bulatman <bulatman@yandex-team.com> | 2023-05-23 11:35:30 +0300 |
commit | a78d78343219fc5d684194b98658dfd66622075d (patch) | |
tree | d02e22827725ce36ede7b1670be76eef32f428c8 /library | |
parent | 3f7fa567faabe3ad86fc4bbdead8d794317c8023 (diff) | |
download | ydb-a78d78343219fc5d684194b98658dfd66622075d.tar.gz |
YT: Fix memory leak in New
Diffstat (limited to 'library')
-rw-r--r-- | library/cpp/yt/memory/new-inl.h | 4 | ||||
-rw-r--r-- | library/cpp/yt/memory/unittests/intrusive_ptr_ut.cpp | 17 |
2 files changed, 19 insertions, 2 deletions
diff --git a/library/cpp/yt/memory/new-inl.h b/library/cpp/yt/memory/new-inl.h index 7d80b81d65..53adaecdc0 100644 --- a/library/cpp/yt/memory/new-inl.h +++ b/library/cpp/yt/memory/new-inl.h @@ -114,7 +114,7 @@ Y_FORCE_INLINE T* NewEpilogue(void* ptr, As&& ... args) new (instance) T(std::forward<As>(args)...); CustomInitialize(instance); return instance; - } catch (const std::exception& ex) { + } catch (...) { // Do not forget to free the memory. TFreeMemory<T>::Do(ptr); throw; @@ -167,7 +167,7 @@ Y_FORCE_INLINE TIntrusivePtr<T> SafeConstruct(void* ptr, As&&... args) try { auto* instance = TConstructHelper<T>::Construct(ptr, std::forward<As>(args)...); return TIntrusivePtr<T>(instance, /*addReference*/ false); - } catch (const std::exception& ex) { + } catch (...) { // Do not forget to free the memory. TFreeMemory<T>::Do(ptr); throw; diff --git a/library/cpp/yt/memory/unittests/intrusive_ptr_ut.cpp b/library/cpp/yt/memory/unittests/intrusive_ptr_ut.cpp index 6171dc7aee..ede5deb80a 100644 --- a/library/cpp/yt/memory/unittests/intrusive_ptr_ut.cpp +++ b/library/cpp/yt/memory/unittests/intrusive_ptr_ut.cpp @@ -161,6 +161,18 @@ private: }; +class TObjectWithExceptionInConstructor + : public TRefCounted +{ +public: + TObjectWithExceptionInConstructor() + { + throw int(1); + } + + volatile int Number = 0; +}; + //////////////////////////////////////////////////////////////////////////////// TEST(TIntrusivePtrTest, Empty) @@ -618,6 +630,11 @@ TEST(TIntrusivePtrTest, Serialize) EXPECT_FALSE(ptr); } +TEST(TIntrusivePtrTest, TestObjectConstructionFail) +{ + ASSERT_THROW(New<TObjectWithExceptionInConstructor>(), int); +} + //////////////////////////////////////////////////////////////////////////////// } // namespace |