You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Memory leak when exception is thrown in adl_serializer::to_json #3881
Throwing an exception in serializer function after the output json object is already initialized results in a memory leak (detected with both ASan and valgrind):
This is throwing inside the constructor. When the constructor doesn't complete without exception, then the destructor is not run, meaning that m_value.destroy(m_type); is not called. Cleaning up from this is going to be tricky. https://isocpp.org/wiki/faq/exceptions#selfcleaning-members
Off the top of my head, the constructor code that calls out to the adl serializer could detect an exception and call m_value.destroy(m_type); manually before rethrowing.
This can be solved by either using a base class or a member var to do the cleanup.
I would prefer one of those solutions, since they solve the problem of leaking memory permanently.
I will implement a solution using a base class in #3901.
/edit
I went with the solution using a member var, since it requires less changes to the existing code and the resulting code is simpler to understand.
Activity
gregmarr commentedon Dec 14, 2022
This is throwing inside the constructor. When the constructor doesn't complete without exception, then the destructor is not run, meaning that
m_value.destroy(m_type);is not called. Cleaning up from this is going to be tricky.https://isocpp.org/wiki/faq/exceptions#selfcleaning-members
Off the top of my head, the constructor code that calls out to the adl serializer could detect an exception and call
m_value.destroy(m_type);manually before rethrowing.Add unit test for leaking memory when throwing inside of an adl seria…
barcode commentedon Dec 27, 2022
This can be solved by either using a base class or a member var to do the cleanup.
I would prefer one of those solutions, since they solve the problem of leaking memory permanently.
The stl uses base classes for containers managing memory (https://github.com/gcc-mirror/gcc/blob/7c755fd9018821b79ddc32ee507897860510986c/libstdc%2B%2B-v3/include/bits/stl_vector.h#L423).
If we use a base class, we do not need to change the variable names. It would be enough to move some code into this base class.
I will implement a solution using a base class in #3901.
/edit
I went with the solution using a member var, since it requires less changes to the existing code and the resulting code is simpler to understand.
Add unit test for leaking memory when throwing inside of an adl seria…
Add unit test for leaking memory when throwing inside of an adl seria…
Add unit test for leaking memory when throwing inside of an adl seria…
Add unit test for leaking memory when throwing inside of an adl seria…
Add unit test for leaking memory when throwing inside of an adl seria…