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
I'd like to use Unity in combination with Address Sanitizer, the problem is that by default ASAN halts the program as soon that it detects a memory error, so failed assertions in tests like:
allocate some memory
some Unity assertions that might fail
free allocated memory
are never reported as failed assertions because ASAN kicks in and halts the program reporting a memory leak. This is misleading because the cause of the failed assertion is lost.
I know I can instruct ASAN to continue after the error, instead of halting (see FAQs; but it's marked as experimental.
I was wondering whether there is a "correct" way to use ASAN with unity, or it's just a bad idea to combine their use. The ideal would be to be able to specify some cleanup code associated with the (group of) assertions, but I cannot find out how this can be done in Unity at the assertion level.
I'm using Unity and ASAN together to check programming assignments, so it makes sense to check also for memory errors in the unit test, and I'd rather prefer avoiding the duplication of the tests.
I often do not use sanitizers directly in my local dev workflow as they slow down tests significantly, but instead rely on a CI action to build and test with ASAN enabled. But the problem you describe about masking a test failure is indeed valid.
My solution for this is to basically disable LSAN whenever a Unity test assertion fails. That way a failed assertion gets reported normally, but when all tests pass and I have a memory leak somewhere, LSAN reports it at the end.
To do this, you can define the UNITY_TEST_ABORT macro which sets a flag and then have the __lsan_is_turned_off() hook check that flag to disable LSAN if there was a failed assertion.
If you're interested, you can check a working solution in this PR that I've created for one of my own projects.
Activity
kilo52 commentedon Jul 22, 2026
I often do not use sanitizers directly in my local dev workflow as they slow down tests significantly, but instead rely on a CI action to build and test with ASAN enabled. But the problem you describe about masking a test failure is indeed valid.
My solution for this is to basically disable LSAN whenever a Unity test assertion fails. That way a failed assertion gets reported normally, but when all tests pass and I have a memory leak somewhere, LSAN reports it at the end.
To do this, you can define the
UNITY_TEST_ABORTmacro which sets a flag and then have the__lsan_is_turned_off()hook check that flag to disable LSAN if there was a failed assertion.If you're interested, you can check a working solution in this PR that I've created for one of my own projects.