|
From: Michael M. <mos...@gm...> - 2017-11-09 09:27:33
|
Hi,
I have a short program that overrides global new and delete operators. When
running it in valgrind 3.13.0 then the global new/delete operators of the
application are not called, in valgrind 3.10.1 the global new/delete
operators are called. My questions are: why this behavior in valgrind
3.13.0 ? how can we get valgrind3.13.0 to call the global new/delete of
the application, is there a configuration setting/command line switch that
enables this? (environment OS Linux, Ubuntu 14.04.5 LTS, x86_64)
The program.
#include <malloc.h>
#include <new>
void* operator new (size_t size_i) throw (std::bad_alloc)
{
fprintf(stderr,"new %d (operator new (size_t size_i) throw
(std::bad_alloc))\n", __LINE__);
return malloc(size_i);
}
void* operator new (size_t size_i, const std::nothrow_t& nothrow_value)
throw()
{
fprintf(stderr,"new %d (operator new (size_t size_i, const
std::nothrow_t& nothrow_value) throw())\n", __LINE__);
return malloc(size_i);
}
void* operator new[] (size_t size_i) throw (std::bad_alloc)
{
fprintf(stderr,"new %d (operator new[] (size_t size_i) throw
(std::bad_alloc)) \n", __LINE__);
return malloc(size_i);
}
void* operator new[] (size_t size_i, const std::nothrow_t&
nothrow_value) throw()
{
fprintf(stderr,"new %d (operator new[] (size_t size_i, const
std::nothrow_t& nothrow_value) throw()) \n", __LINE__);
return malloc(size_i);
}
void operator delete (void* p_i) throw()
{
fprintf(stderr, "delete %d (operator delete (void* p_i)
throw())\n",__LINE__);
free(p_i);
}
void operator delete (void* p_i, const std::nothrow_t&
nothrow_constant) throw()
{
fprintf(stderr, "delete %d\n",__LINE__);
free(p_i);
}
void operator delete[] (void* p_i) throw()
{
fprintf(stderr, "delete %d\n",__LINE__);
free(p_i);
}
void operator delete[] (void* p_i, const std::nothrow_t&
nothrow_constant) throw()
{
fprintf(stderr, "delete %d\n",__LINE__);
free(p_i);
}
#include <stdio.h>
main()
{
fprintf(stderr, "ku !\n");
char * a = new char[123];
delete [] a;
int * b = new int();
delete b;
return 0;
}
Now compile it
gcc -g valg.cpp -lstdc++
run it without valgrind
$ ./a.out
ku !
new 19 (operator new[] (size_t size_i) throw (std::bad_alloc))
delete 43
new 6 (operator new (size_t size_i) throw (std::bad_alloc))
delete 31 (operator delete (void* p_i) throw())
With valgrind 3.13.0 global operator new is not called.
valgrind ./a.out
==24181== Memcheck, a memory error detector
==24181== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et
al.
==24181== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright
info
==24181== Command: ./a.out
==24181==
ku !
==24181==
with valgrind 3.10.1
valgrind ./a.out
==32055== Memcheck, a memory error detector
==32055== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==32055== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==32055== Command: ./a.out
==32055==
ku !
new 19 (operator new[] (size_t size_i) throw (std::bad_alloc))
delete 43
new 6 (operator new (size_t size_i) throw (std::bad_alloc))
delete 31 (operator delete (void* p_i) throw())
==32055==
==32055== HEAP SUMMARY:
==32055== in use at exit: 0 bytes in 0 blocks
==32055== total heap usage: 2 allocs, 2 frees, 127 bytes allocated
==32055==
==32055== All heap blocks were freed -- no leaks are possible
==32055==
==32055== For counts of detected and suppressed errors, rerun with: -v
==32055== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
|