Romain Picard
unread,Jun 14, 2012, 5:47:57 AM6/14/12Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to cppu...@googlegroups.com
Hello everybody,
On the cpputest website I did not find where we can submit patches, so
I try here.
The following patch adds support for new(std:nothrow) variants of the
new operator in the memory leak detector.
CppUTest mistakenly detects leaks on code that use "new(std:nothrow)"
without this change.
Romain.
-----------------------------------------------
diff -urN
CppUTest-v3.1.ori/include/CppUTest/MemoryLeakDetectorNewMacros.h
CppUTest-v3.1/include/CppUTest/MemoryLeakDetectorNewMacros.h
---
CppUTest-v3.1.ori/include/CppUTest/MemoryLeakDetectorNewMacros.h 2012-05-14
14:48:20.000000000 +0200
+++
CppUTest-v3.1/include/CppUTest/MemoryLeakDetectorNewMacros.h 2012-05-14
17:04:54.000000000 +0200
@@ -54,9 +54,13 @@
#include <string>
void* operator new(size_t size, const char* file, int line) throw
(std::bad_alloc);
+ void* operator new(size_t size, const std::nothrow_t&, const char*
file, int line) throw ();
void* operator new[](size_t size, const char* file, int line) throw
(std::bad_alloc);
+ void* operator new[](size_t size, const std::nothrow_t&, const
char* file, int line) throw ();
void* operator new(size_t size) throw(std::bad_alloc);
+ void* operator new(size_t size, const std::nothrow_t&) throw();
void* operator new[](size_t size) throw(std::bad_alloc);
+ void* operator new[](size_t size, const std::nothrow_t&) throw();
#else
diff -urN CppUTest-v3.1.ori/include/CppUTest/MemoryLeakWarningPlugin.h
CppUTest-v3.1/include/CppUTest/MemoryLeakWarningPlugin.h
---
CppUTest-v3.1.ori/include/CppUTest/MemoryLeakWarningPlugin.h 2012-05-14
14:48:20.000000000 +0200
+++
CppUTest-v3.1/include/CppUTest/MemoryLeakWarningPlugin.h 2012-05-14
17:04:54.000000000 +0200
@@ -53,8 +53,11 @@
#if CPPUTEST_USE_STD_CPP_LIB
#include <new>
+
void* operator new(size_t size) throw(std::bad_alloc);
+void* operator new(size_t size, const std::nothrow_t&) throw();
void* operator new[](size_t size) throw(std::bad_alloc);
+void* operator new[](size_t size, const std::nothrow_t&) throw();
void operator delete(void* mem) throw();
void operator delete[](void* mem) throw();
diff -urN CppUTest-v3.1.ori/src/CppUTest/MemoryLeakWarningPlugin.cpp
CppUTest-v3.1/src/CppUTest/MemoryLeakWarningPlugin.cpp
---
CppUTest-v3.1.ori/src/CppUTest/MemoryLeakWarningPlugin.cpp 2012-05-14
14:48:20.000000000 +0200
+++ CppUTest-v3.1/src/CppUTest/MemoryLeakWarningPlugin.cpp 2012-05-14
17:04:54.000000000 +0200
@@ -51,6 +51,12 @@
return memory;
}
+static void* mem_leak_operator_new_nothrow (size_t size)
UT_THROW_EMPTY()
+{
+ void* memory =
MemoryLeakWarningPlugin::getGlobalDetector()->allocMemory(getCurrentNewAllocator(),
size);
+ return memory;
+}
+
static void* mem_leak_operator_new_debug (size_t size, const char*
file, int line) UT_THROW(std::bad_alloc)
{
void *memory =
MemoryLeakWarningPlugin::getGlobalDetector()->allocMemory(getCurrentNewAllocator(),
size, (char*) file, line);
@@ -58,6 +64,12 @@
return memory;
}
+static void* mem_leak_operator_new_debug_nothrow (size_t size, const
char* file, int line) UT_THROW_EMPTY()
+{
+ void *memory =
MemoryLeakWarningPlugin::getGlobalDetector()->allocMemory(getCurrentNewAllocator(),
size, (char*) file, line);
+ return memory;
+}
+
static void* mem_leak_operator_new_array (size_t size)
UT_THROW(std::bad_alloc)
{
void* memory =
MemoryLeakWarningPlugin::getGlobalDetector()->allocMemory(getCurrentNewArrayAllocator(),
size);
@@ -65,6 +77,12 @@
return memory;
}
+static void* mem_leak_operator_new_array_nothrow (size_t size)
UT_THROW_EMPTY()
+{
+ void* memory =
MemoryLeakWarningPlugin::getGlobalDetector()->allocMemory(getCurrentNewArrayAllocator(),
size);
+ return memory;
+}
+
static void* mem_leak_operator_new_array_debug (size_t size, const
char* file, int line) UT_THROW(std::bad_alloc)
{
void* memory =
MemoryLeakWarningPlugin::getGlobalDetector()->allocMemory(getCurrentNewArrayAllocator(),
size, (char*) file, line);
@@ -72,6 +90,12 @@
return memory;
}
+static void* mem_leak_operator_new_array_debug_nothrow (size_t size,
const char* file, int line) UT_THROW(std::bad_alloc)
+{
+ void* memory =
MemoryLeakWarningPlugin::getGlobalDetector()->allocMemory(getCurrentNewArrayAllocator(),
size, (char*) file, line);
+ return memory;
+}
+
static void mem_leak_operator_delete (void* mem) UT_THROW_EMPTY()
{
MemoryLeakWarningPlugin::getGlobalDetector()->invalidateMemory((char*)
mem);
@@ -89,21 +113,41 @@
return PlatformSpecificMalloc(size);
}
+static void* normal_operator_new_nothrow (size_t size)
UT_THROW_EMPTY()
+{
+ return PlatformSpecificMalloc(size);
+}
+
static void* normal_operator_new_debug (size_t size, const char*
/*file*/, int /*line*/) UT_THROW(std::bad_alloc)
{
return PlatformSpecificMalloc(size);
}
+static void* normal_operator_new_debug_nothrow (size_t size, const
char* /*file*/, int /*line*/) UT_THROW_EMPTY()
+{
+ return PlatformSpecificMalloc(size);
+}
+
static void* normal_operator_new_array (size_t size)
UT_THROW(std::bad_alloc)
{
return PlatformSpecificMalloc(size);
}
+static void* normal_operator_new_array_nothrow (size_t size)
UT_THROW_EMPTY()
+{
+ return PlatformSpecificMalloc(size);
+}
+
static void* normal_operator_new_array_debug (size_t size, const char*
/*file*/, int /*line*/) UT_THROW(std::bad_alloc)
{
return PlatformSpecificMalloc(size);
}
+static void* normal_operator_new_array_debug_nothrow (size_t size,
const char* /*file*/, int /*line*/) UT_THROW_EMPTY()
+{
+ return PlatformSpecificMalloc(size);
+}
+
static void normal_operator_delete (void* mem) UT_THROW_EMPTY()
{
PlatformSpecificFree(mem);
@@ -115,9 +159,13 @@
}
static void *(*operator_new_fptr)(size_t size)
UT_THROW(std::bad_alloc) = mem_leak_operator_new;
+static void *(*operator_new_nothrow_fptr)(size_t size)
UT_THROW_EMPTY() = mem_leak_operator_new_nothrow;
static void *(*operator_new_debug_fptr)(size_t size, const char* file,
int line) UT_THROW(std::bad_alloc) = mem_leak_operator_new_debug;
+static void *(*operator_new_debug_nothrow_fptr)(size_t size, const
char* file, int line) UT_THROW_EMPTY() =
mem_leak_operator_new_debug_nothrow;
static void *(*operator_new_array_fptr)(size_t size)
UT_THROW(std::bad_alloc) = mem_leak_operator_new_array;
+static void *(*operator_new_array_nothrow_fptr)(size_t size)
UT_THROW_EMPTY() = mem_leak_operator_new_array_nothrow;
static void *(*operator_new_array_debug_fptr)(size_t size, const char*
file, int line) UT_THROW(std::bad_alloc) =
mem_leak_operator_new_array_debug;
+static void *(*operator_new_array_debug_nothrow_fptr)(size_t size,
const char* file, int line) UT_THROW_EMPTY() =
mem_leak_operator_new_array_debug_nothrow;
static void (*operator_delete_fptr)(void* mem) UT_THROW_EMPTY() =
mem_leak_operator_delete;
static void (*operator_delete_array_fptr)(void* mem) UT_THROW_EMPTY()
= mem_leak_operator_delete_array;
@@ -126,11 +174,21 @@
return operator_new_fptr(size);
}
+void* operator new(size_t size, const std::nothrow_t&)
UT_THROW_EMPTY()
+{
+ return operator_new_nothrow_fptr(size);
+}
+
void* operator new(size_t size, const char* file, int line)
UT_THROW(std::bad_alloc)
{
return operator_new_debug_fptr(size, file, line);
}
+void* operator new(size_t size, const std::nothrow_t&, const char*
file, int line) UT_THROW_EMPTY()
+{
+ return operator_new_debug_nothrow_fptr(size, file, line);
+}
+
void operator delete(void* mem) UT_THROW_EMPTY()
{
operator_delete_fptr(mem);
@@ -141,11 +199,21 @@
return operator_new_array_fptr(size);
}
+void* operator new[](size_t size, const std::nothrow_t&)
UT_THROW_EMPTY()
+{
+ return operator_new_array_nothrow_fptr(size);
+}
+
void* operator new [](size_t size, const char* file, int line)
UT_THROW(std::bad_alloc)
{
return operator_new_array_debug_fptr(size, file, line);
}
+void* operator new [](size_t size, const std::nothrow_t&, const char*
file, int line) UT_THROW_EMPTY()
+{
+ return operator_new_array_debug_nothrow_fptr(size, file, line);
+}
+
void operator delete[](void* mem) UT_THROW_EMPTY()
{
operator_delete_array_fptr(mem);
@@ -157,9 +225,13 @@
{
#if CPPUTEST_USE_MEM_LEAK_DETECTION
operator_new_fptr = normal_operator_new;
+ operator_new_nothrow_fptr = normal_operator_new_nothrow;
operator_new_debug_fptr = normal_operator_new_debug;
+ operator_new_debug_nothrow_fptr = normal_operator_new_debug_nothrow;
operator_new_array_fptr = normal_operator_new_array;
+ operator_new_array_nothrow_fptr = normal_operator_new_array_nothrow;
operator_new_array_debug_fptr = normal_operator_new_array_debug;
+ operator_new_array_debug_nothrow_fptr =
normal_operator_new_array_debug_nothrow;
operator_delete_fptr = normal_operator_delete;
operator_delete_array_fptr = normal_operator_delete_array;
#endif
@@ -169,9 +241,13 @@
{
#if CPPUTEST_USE_MEM_LEAK_DETECTION
operator_new_fptr = mem_leak_operator_new;
+ operator_new_nothrow_fptr = mem_leak_operator_new_nothrow;
operator_new_debug_fptr = mem_leak_operator_new_debug;
+ operator_new_debug_nothrow_fptr =
mem_leak_operator_new_debug_nothrow;
operator_new_array_fptr = mem_leak_operator_new_array;
+ operator_new_array_nothrow_fptr =
mem_leak_operator_new_array_nothrow;
operator_new_array_debug_fptr = mem_leak_operator_new_array_debug;
+ operator_new_array_debug_nothrow_fptr =
mem_leak_operator_new_array_debug_nothrow;
operator_delete_fptr = mem_leak_operator_delete;
operator_delete_array_fptr = mem_leak_operator_delete_array;
#endif