[patch] Support for std::nothrow variants of new

476 views
Skip to first unread message

Romain Picard

unread,
Jun 14, 2012, 5:47:57 AM6/14/12
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

Romain Picard

unread,
Jun 14, 2012, 5:56:21 AM6/14/12
to cppu...@googlegroups.com
Sorry the patch was not correctly sent.

Adding it as an enclosure.
Romain.
new_nothrow.patch

Bas Vodde

unread,
Jun 14, 2012, 7:03:05 PM6/14/12
to cppu...@googlegroups.com

Kewl. I'll try to integrate that probably within 2 weeks if thats ok.

Oh, it is possible to submit patches on sourceforge, but this list will do fine too :)

Tnx!

Bas

Bas Vodde

unread,
Jun 17, 2012, 12:57:09 AM6/17/12
to cppu...@googlegroups.com, Romain Picard

Hi Romain,

Uhm, I've tried integrating your path, but had some problems which leads me to some questions to you :)

I noticed that the patch didn't include any tests. We usually don't include code without tests except when it is absolutely impossible to test (which is rarely ever true).

So, I started integrating the patch with writing tests for the new nothrow operation, but I actually failed with that already. The test I wanted to write it:

TEST(OutOfMemoryTestsForOperatorNew, FailingNewOperatorWithDontThrowReturnsNull)
{
POINTERS_EQUAL(NULL, new(std::nothrow));
}

This failed because operator new is #defined in order to be able to add debug information to it. So the pre-processor changes the call to:

TEST(OutOfMemoryTestsForOperatorNew, FailingNewOperatorWithDontThrowReturnsNull)
{
POINTERS_EQUAL(NULL, new(__FILE__, __LINE__)(std::nothrow));
}

Which is invalid C++ and it doesn't like it. The only way I can call the nothrow variant is to #undef new, but that means it would only call the one without the __FILE__ and __LINE__ parameters, unless I call that directly. I sort-of assume that people won't #undef the new and then call the new operator with new (std::nothrow, __FILE__, __LINE__).... which would make that overload useless :(

So, questions to you:

- How did you actually use the nothrow operator when using CppuTest?
- Similar question from a different angle: What lead you to adding these additional operator overloads?

The only way my small brain could imagine how to get it to work would be to add a new macro such as:
#define new_nothrow new(std::nothrow, __FILE__, __LINE__)

but that would require the production code under test to use the new_nothrow macros rather than the Std C++ way of calling it.
(this would anyway be required as the new #define in CppUTest will always make a mess of nothrow calls, unfortunately. It is the painful balance of adding additional debugging information vs staying away from the preprocessor)

I'll be waiting for the answer to define how to integrate (and test!) the patch.

Tnx

Bas
> <new_nothrow.patch>

Romain Picard

unread,
Jul 6, 2012, 4:24:05 AM7/6/12
to cppu...@googlegroups.com, Bas Vodde
> Uhm, I've tried integrating your path, but had some problems which
> leads me to some questions to you :)
>
> I noticed that the patch didn't include any tests. We usually don't
> include code without tests except when it is absolutely impossible to
> test (which is rarely ever true).

Yes, I was expecting that remark from you :)

>[...]
> So, questions to you:
>
> - How did you actually use the nothrow operator when using CppuTest?

I use it the way you did when trying to write a test for this patch:
foo = new(std::nothrow) MyClass;

> - Similar question from a different angle: What lead you to adding
> these additional operator overloads?

I use the nothrow version of new in all my code. When I write some
tests on such a code, the allocations are not catch by cpputest. So at
the end of the tests, cpputest wrongly detects memory leaks.

I wrote a small test case that leads to my issue:
---------------------------------
#include <new>
#include "CppUTest/TestHarness.h"

class Foo
{
public:
Foo(void) {};
int Bar;
};

TEST_GROUP(NoThrow)
{
};

TEST(NoThrow, New)
{
Foo *Object= new(std::nothrow) Foo();
delete Object;
}
---------------------------------

Running it leads to the following failure:
AllTests/NewNoThrowTest.cpp:15: error: Failure in TEST(NoThrow, New)
src/CppUTest/MemoryLeakWarningPlugin.cpp:198: error:
Deallocating non-allocated memory
allocated at file: <unknown> line: 0 size: 0 type: unknown
deallocated at file: <unknown> line: 0 type: delete

I think that i understand why I did not see your issue: I do not use
"-include
$(CPPUTEST_HOME)/include/CppUTest/MemoryLeakDetectorNewMacros.h" in my
makefiles. I now confirm that my patch does not work with the new macro
redefinition :(


> The only way my small brain could imagine how to get it to work would
> be to add a new macro such as:
> #define new_nothrow new(std::nothrow, __FILE__, __LINE__)
>
> but that would require the production code under test to use the
> new_nothrow macros rather than the Std C++ way of calling it.
> (this would anyway be required as the new #define in CppUTest will
> always make a mess of nothrow calls, unfortunately. It is the painful
> balance of adding additional debugging information vs staying away
> from the preprocessor)

The macro may be defined the following way:
# define new(...) new(__VA_ARGS__, __FILE__, __LINE__)

This should work for both new and new(std::nothrow), and add the throw
type before the file and line arguments.
If this seems a correct solution to you, I can submit a new patch
fixing the new macro, and some tests.

Romain.


Bas Vodde

unread,
Jul 7, 2012, 7:29:56 AM7/7/12
to Romain Picard, cppu...@googlegroups.com

Hiya,

Thanks for the clarification. I think I'll add the non-debug new overload for nothow. I'll leave the debug one for now as... well.. it seems it is uncallable. :(

> The macro may be defined the following way:
> # define new(...) new(__VA_ARGS__, __FILE__, __LINE__)
>
> This should work for both new and new(std::nothrow), and add the throw
> type before the file and line arguments.
> If this seems a correct solution to you, I can submit a new patch
> fixing the new macro, and some tests.

I'd love to see the patch :) I'm not too sure it works.

The problem is that new is not a method call-style function.

So, if you do:

int a = new int;

THen you don't add the ().

So, therefore if you use the #define new(...) then it will still give a compiler error (at least, for me it does).

Thats why the #define for new is not a new() but a #define new.

So.... I don't think that variadic macros work :(

Did you get it to work?

Thanks,

Bas

ps. Additionally, variadic macros is C99 and in CppUTest it failed due to all the error flags that are turned on :)



>
> Romain.
>
>

Romain Picard

unread,
Jul 7, 2012, 11:02:30 AM7/7/12
to Bas Vodde, cppu...@googlegroups.com
>> The macro may be defined the following way:
>> # define new(...) new(__VA_ARGS__, __FILE__, __LINE__)
>>
>> This should work for both new and new(std::nothrow), and add the throw
>> type before the file and line arguments.
>> If this seems a correct solution to you, I can submit a new patch
>> fixing the new macro, and some tests.
>
> I'd love to see the patch :) I'm not too sure it works.
>
> The problem is that new is not a method call-style function.
>
> So, if you do:
>
> int a = new int;
>
> THen you don't add the ().
>
> So, therefore if you use the #define new(...) then it will still give
> a compiler error (at least, for me it does).
>
> Thats why the #define for new is not a new() but a #define new.
>
> So.... I don't think that variadic macros work :(
>
> Did you get it to work?

I only tried to run the preprocessor on it (gcc -E) and it seemed fine
to me: I think that the () were removed when no parameter was provided
but maybe I am wrong. I will do more tests to see if I can find a
solution.

Romain.

Bas Vodde

unread,
Jul 8, 2012, 1:41:34 AM7/8/12
to Romain Picard, cppu...@googlegroups.com

Hiya,

>> So.... I don't think that variadic macros work :(
>>
>> Did you get it to work?
>
> I only tried to run the preprocessor on it (gcc -E) and it seemed fine
> to me: I think that the () were removed when no parameter was provided
> but maybe I am wrong. I will do more tests to see if I can find a
> solution.

Ok. In the mean time, I'll intergrate the nothrow operator new without debug info as that one seems to be the one you needed.

Thanks,

Bas

>
> Romain.

Bas Vodde

unread,
Jul 8, 2012, 8:32:52 AM7/8/12
to Romain Picard, cppu...@googlegroups.com

Hola again,

>> So, questions to you:
>>
>> - How did you actually use the nothrow operator when using CppuTest?
>
> I use it the way you did when trying to write a test for this patch:
> foo = new(std::nothrow) MyClass;

Oki. But, as said, that doesn't work with the #define new :)
(see later mails)

>> - Similar question from a different angle: What lead you to adding
>> these additional operator overloads?
>
> I use the nothrow version of new in all my code. When I write some
> tests on such a code, the allocations are not catch by cpputest. So at
> the end of the tests, cpputest wrongly detects memory leaks.
>
> I wrote a small test case that leads to my issue:
> ---------------------------------
> #include <new>
> #include "CppUTest/TestHarness.h"
>
> class Foo
> {
> public:
> Foo(void) {};
> int Bar;
> };
>
> TEST_GROUP(NoThrow)
> {
> };
>
> TEST(NoThrow, New)
> {
> Foo *Object= new(std::nothrow) Foo();
> delete Object;
> }

Thanks. The test is lacking an assert though, but I've added some similar tests that check a bit more.

> ---------------------------------
>
> Running it leads to the following failure:
> AllTests/NewNoThrowTest.cpp:15: error: Failure in TEST(NoThrow, New)
> src/CppUTest/MemoryLeakWarningPlugin.cpp:198: error:
> Deallocating non-allocated memory
> allocated at file: <unknown> line: 0 size: 0 type: unknown
> deallocated at file: <unknown> line: 0 type: delete

*grin* :)

> I think that i understand why I did not see your issue: I do not use
> "-include
> $(CPPUTEST_HOME)/include/CppUTest/MemoryLeakDetectorNewMacros.h" in my
> makefiles. I now confirm that my patch does not work with the new macro
> redefinition :(

Yup. Thats the case. That means the debug info version does actually never get called.

>
> The macro may be defined the following way:
> # define new(...) new(__VA_ARGS__, __FILE__, __LINE__)

It would be great if we can get this to work, but the difference between a macro (not used in the new case) and a symbollic constant (used in the new case) will probably make this solution impossible.

However, I've now checked in similar code as your patch on the trunk. A couple additional tests and removed the debug variant as I haven't figured out how it can be useful :)

Let me know if this will work for you?

Thanks!

Bas



Bas Vodde

unread,
Jul 8, 2012, 8:33:18 AM7/8/12
to cppu...@googlegroups.com
As mentioned in the longer mail. Integrated by intent :P

Bas

On 14-Jun-2012, at 5:56 PM, Romain Picard wrote:

> <new_nothrow.patch>

Romain Picard

unread,
Jul 10, 2012, 11:15:24 AM7/10/12
to Bas Vodde, cppu...@googlegroups.com
>> The macro may be defined the following way:
>> # define new(...) new(__VA_ARGS__, __FILE__, __LINE__)
>
> It would be great if we can get this to work, but the difference
> between a macro (not used in the new case) and a symbollic constant
> (used in the new case) will probably make this solution impossible.

I think I found something that should work:
http://nvwa.cvs.sourceforge.net/viewvc/nvwa/nvwa/debug_new.cpp?view=markup

This projects uses a template operator to retrieve the alloced address
and track the file and line values.
This should work for all new variants.

> However, I've now checked in similar code as your patch on the trunk.
> A couple additional tests and removed the debug variant as I haven't
> figured out how it can be useful :)
>
> Let me know if this will work for you?

The current svn trunk works fine for me. Thanks.


Romain Picard

unread,
Jul 10, 2012, 11:16:21 AM7/10/12
to Bas Vodde, cppu...@googlegroups.com
>>> The macro may be defined the following way:
>>> # define new(...) new(__VA_ARGS__, __FILE__, __LINE__)
>>
>> It would be great if we can get this to work, but the difference
>> between a macro (not used in the new case) and a symbollic constant
>> (used in the new case) will probably make this solution impossible.
>
> I think I found something that should work:
> http://nvwa.cvs.sourceforge.net/viewvc/nvwa/nvwa/debug_new.cpp?view=markup
>

Sorry, the interesting file is this one:
http://nvwa.cvs.sourceforge.net/viewvc/nvwa/nvwa/debug_new.h?view=markup

Bas Vodde

unread,
Jul 10, 2012, 8:33:32 PM7/10/12
to cppu...@googlegroups.com

Interesting...

Never seen that technique used before. Looks like it might work, though don't like the template usage (CppUTest doesn't use templates by design!)

I will have to look in this.

Bas

Bas Vodde

unread,
Aug 24, 2012, 5:36:24 AM8/24/12
to cppu...@googlegroups.com

Hi Romain,

The implementation is an interesting hack of the C++ language :P

I've put this as an enhancement on github so we won't forget it. Didn't implement it yet though.

Thanks and much appreciated.

Bas

Venkatesh Krishna mohan

unread,
May 23, 2013, 12:07:47 AM5/23/13
to cppu...@googlegroups.com
I Use CppUTest v 3.3. But still i see this issue.
cpputest/tests/th_ssize.cpp:61: error: Failure in TEST(SSizeGroup, TEST_SSize_setHeight)

TEST_SSize_setHeight:61: error:

        Deallocating non-allocated memory

   allocated at file: <unknown> line: 0 size: 0 type: unknown

   deallocated at file: <unknown> line: 0 type: delete

I am a novice user of CppUTest.
Please help me fix this issue.

Thanks in Advance.

PS: I am sure there is not such issue in other builds of my source code. Tried with valgrind to see memory leaks and it says no leaks. Kindly help.

Bas Vodde

unread,
May 27, 2013, 12:34:07 AM5/27/13
to cppu...@googlegroups.com

Hi Venkatesh,

(I assume you are the same person as who opened a github issue about this, right?)

Deleting non-allocated memory happens when you do "delete" on a piece of memory more than once. I've often seen this and people often claim it is a bug… but more of the time the bug is in their code :)

What happens in CppUTest is the following:

-> When you new some memory, it will keep track of it.
-> When you delete the memory, it will 'release' the memory accounting information too.
-> When you delete some memory that was not 'newed' then you will get this error.

The most common reason is that some memory was deleted twice.

The way to debug this is to check the th_ssize.cpp line 61 where the delete is happening. Find the new that related to it and study both the new and the delete. If you still can't find the problem, then set a breakpoint on the new and one on the delete and run it. If you *still* can't find it, make sure the pointer value is the same…

Hope this helps and let me know if you resolved this!

Thanks,

Bas
> --
> You received this message because you are subscribed to the Google Groups "cpputest" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to cpputest+u...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

SPC

unread,
Jan 24, 2014, 3:44:03 PM1/24/14
to cppu...@googlegroups.com
What is the status of this issue in v3.5? I have code where I use the nothrow variant of new and everything was fine, except when I created a failing test where I don't delete the allocated memory at which point the leak detector reports the leak, but fails to identify the source of the allocation. I assume this is because I've not included MemoryLeakDetectorNewMacros.h. However, when I include this header file I get multiple compilation errors at each nothrow allocation. Is there a way to use the nothrow new with CppUTest and get proper reporting of leaks?

Bas Vodde

unread,
Jan 25, 2014, 9:36:17 PM1/25/14
to cppu...@googlegroups.com

Hi,

I don’t think there is an easy way for that at the moment. You’ll need to #undef new before the nothrow call. But then the nothrow call won’t contain the additional memory leak information. Unless you change the actual call to new, but I guess this is all in production code and you wouldn’t want to do that :(

So, at the moment, I actually don’t know a good way of doing that.

Bas
Reply all
Reply to author
Forward
0 new messages