Skip to content

Commit eb5eac7

Browse files
authored
Merge pull request #1868 from basvodde/master
Moved the allocation outside the compile unit to avoid optimising the new away
2 parents 2ad47d2 + 8e1665e commit eb5eac7

File tree

3 files changed

+38
-17
lines changed

3 files changed

+38
-17
lines changed

tests/CppUTest/AllocationInCppFile.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,34 @@ char* newArrayAllocationWithoutMacro()
2727
return new char[100];
2828
}
2929

30+
int* newIntAllocationWithoutMacro()
31+
{
32+
return new int;
33+
}
34+
35+
int* newIntNoThowAllocationWithoutMacro()
36+
{
37+
#if CPPUTEST_USE_STD_CPP_LIB
38+
return new (std::nothrow) int;
39+
#else
40+
return new int;
41+
#endif
42+
}
43+
44+
char* newCharArrayAllocationWithoutMacro()
45+
{
46+
return new char[20];
47+
}
48+
49+
char* newCharArrayNoThrowAllocationWithoutMacro()
50+
{
51+
#if CPPUTEST_USE_STD_CPP_LIB
52+
return new (std::nothrow) char[20];
53+
#else
54+
return new char[20];
55+
#endif
56+
}
57+
3058
#if CPPUTEST_HAVE_EXCEPTIONS
3159

3260
ClassThatThrowsAnExceptionInTheConstructor::ClassThatThrowsAnExceptionInTheConstructor()

tests/CppUTest/AllocationInCppFile.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ char* newArrayAllocation();
77
char* newAllocationWithoutMacro();
88
char* newArrayAllocationWithoutMacro();
99

10+
int* newIntAllocationWithoutMacro();
11+
int* newIntNoThowAllocationWithoutMacro();
12+
char* newCharArrayAllocationWithoutMacro();
13+
char* newCharArrayNoThrowAllocationWithoutMacro();
14+
1015
#if CPPUTEST_HAVE_EXCEPTIONS
1116

1217
class ClassThatThrowsAnExceptionInTheConstructor

tests/CppUTest/MemoryLeakWarningTest.cpp

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "CppUTest/TestHarness_c.h"
3636
#include "CppUTest/SimpleMutex.h"
3737
#include "DummyMemoryLeakDetector.h"
38+
#include "AllocationInCppFile.h"
3839

3940
TEST_GROUP(MemoryLeakWarningLocalDetectorTest)
4041
{
@@ -471,28 +472,17 @@ TEST(MemoryLeakWarningThreadSafe, turnOnThreadSafeNewDeleteOverloadsDebug)
471472
MemoryLeakWarningPlugin::turnOnDefaultNotThreadSafeNewDeleteOverloads();
472473
}
473474

474-
#ifdef __clang__
475-
476-
IGNORE_TEST(MemoryLeakWarningThreadSafe, turnOnThreadSafeNewDeleteOverloads)
477-
{
478-
/* Clang misbehaves with -O2 - it will not overload operator new or
479-
* operator new[] no matter what. Therefore, this test is must be ignored.
480-
*/
481-
}
482-
483-
#else
484-
485475
TEST(MemoryLeakWarningThreadSafe, turnOnThreadSafeNewDeleteOverloads)
486476
{
487477
#undef new
488478

489479
size_t storedAmountOfLeaks = MemoryLeakWarningPlugin::getGlobalDetector()->totalMemoryLeaks(mem_leak_period_all);
490480
MemoryLeakWarningPlugin::turnOnThreadSafeNewDeleteOverloads();
491481

492-
int *n = new int;
493-
int *n_nothrow = new (std::nothrow) int;
494-
char *str = new char[20];
495-
char *str_nothrow = new (std::nothrow) char[20];
482+
int *n = newIntAllocationWithoutMacro();
483+
int *n_nothrow = newIntNoThowAllocationWithoutMacro();
484+
char *str = newCharArrayAllocationWithoutMacro();
485+
char *str_nothrow = newCharArrayNoThrowAllocationWithoutMacro();
496486

497487
LONGS_EQUAL(storedAmountOfLeaks + 4, MemoryLeakWarningPlugin::getGlobalDetector()->totalMemoryLeaks(mem_leak_period_all));
498488
CHECK_EQUAL(4, mutexLockCount);
@@ -516,5 +506,3 @@ TEST(MemoryLeakWarningThreadSafe, turnOnThreadSafeNewDeleteOverloads)
516506
#endif
517507

518508
#endif
519-
520-
#endif

0 commit comments

Comments
 (0)