Is there a way to write a memory leak detector supporting new(nothrow)?
For example,
#include <My_Debug_New.h >
using namespace std;
int main()
{
int* p1 = new int;
int* p2 = new(nothrow) int; // note this!!!
}
Ideally, after running it in debug mode, owing to p1 and p2 are not
deleted, the output window of the IDE should report memory leaks with
source file names and actual line numbers.
Provided that the whole program doesn't use new(nothrow), I can
implement a memory leak detector as follows:
#if _DEBUG
void* operator new(size_t size, char* srcFileName, int nLineNum);
void* operator delete(void* p);
// ......
#define new new(__FILE__, __LINE__)
#endif
However, by using macro, new and new(nothrow) cannot be simultaneouly
supported. My question is: How to implement this feature that can
simultaneously support? Is this feasible?
Thanks in advance for any help. 1 2067
Lighter wrote:
Is there a way to write a memory leak detector supporting new(nothrow)?
For example,
#include <My_Debug_New.h >
using namespace std;
int main()
{
int* p1 = new int;
int* p2 = new(nothrow) int; // note this!!!
}
Ideally, after running it in debug mode, owing to p1 and p2 are not
deleted, the output window of the IDE should report memory leaks with
source file names and actual line numbers.
Provided that the whole program doesn't use new(nothrow), I can
implement a memory leak detector as follows:
#if _DEBUG
void* operator new(size_t size, char* srcFileName, int nLineNum);
void* operator delete(void* p);
// ......
#define new new(__FILE__, __LINE__)
#endif
However, by using macro, new and new(nothrow) cannot be simultaneouly
supported. My question is: How to implement this feature that can
simultaneously support? Is this feasible?
Thanks in advance for any help.
Overloading new and delete to detect memory leaks is a brittle solution.
Using macros to redefine a keyword is more brittle. At that point you
are relying on clients to correctly use one thing (your redefined new)
to detect incorrectly using another thing (ironically, the same thing
you are relying on them to use correctly).
A better solution is to use some memory debugger like Valgrind or
Purify. There is a list of memory debuggers on wikipedia: http://en.wikipedia.org/wiki/Memory_debugger
--
Alan Johnson This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Koen |
last post by:
Hi!
What is the method you use to handle memory allocation failure in C++
using the "new" operator?
I used to only check if a NULL pointer is returned after the "new" call,
but that's probably only working in MSVC++.
I also read about the "bad_alloc" exception, but that doesn't seem to be
thrown by MSVC++.
Now, one could of course say "don't use MSVC++", but I'd rather like to
hear a good solution for being able to handle memory...
|
by: Newsnet Customer |
last post by:
Hi,
consider this:
void test ()
{
A* anA = new A();
if (anA == 0){//handle situation}
}
|
by: Dave |
last post by:
Is it possible for the nothrow form of the new operator to ever throw?
|
by: Roger Davis |
last post by:
I am trying to use nothrow new() and am encountering what seems
to be a bug in the SGI/Irix C++ environment. The following
program runs OK under Solaris and Linux, but dumps core at the
delete statement under Irix. Is this really an SGI implementation bug,
or have I done something stupid? (Other than trying to use nothrow
new(), that is ;-) )
#include <new>
#include <stdexcept>
|
by: Alex Vinokur |
last post by:
The memory allocation issue in embedded systems is usually critical..
How can one manage that?
1. Via 'new'
char* p = new (nothrow) char ;
if (p == 0)
{
// He we know that it is impossible to allocate the requested memory
// We can do something relevant.
| |
by: Alex Vinokur |
last post by:
What is difference between
delete p;
and
delete(nothrow)p;
?
--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
|
by: MajorSetback |
last post by:
I am using the Redhat version of Linux and GNU C++.
It is not clear to me whether this is a Linux issue or a C++ issue. I
do not have this problem running the same program on Windows but
tweaking the C++ code appears to fix the problem on Linux.
I have a static array that is declared privately in one class and a
structure that is declared publicly in another class. The program uses
both classes. I was getting core dumps and traced...
|
by: Alex Vinokur |
last post by:
Foo* p = new (nothrow) Foo;
Should we use
operator delete (p, nothrow) ?
Or can we use
delete p; ?
If we use
operator delete (p, nothrow)
how to replace it by operator style?
|
by: Dustan |
last post by:
This is the biggie that has been keeping me from even trying to use C+
+ for more than about 10 seconds: it doesn't work. I've been able to
get java and (of course) python programs to compile and run, but not C+
+.
I copied a program from http://www.cplusplus.com/doc/tutorial/dynamic.html
(if that's a bad place to go for a tutorial, feel free to direct me
elsewhere), and the following line gets an error:
p= new (nothrow) int;
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |