- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// The one and only application object
CWinApp theApp;
using namespace std;
namespace
{
struct A
{
A():x(new int())
{
}
~A()
{
//delete x; // forced memory leak
}
int * const x;
};
A a; // memory leak was not detected
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
_tprintf(_T("Fatal Error: MFC initialization failed\\n"));
nRetCode = 1;
}
else
{
{
A a; // ok - memory leak is detected
*a.x = 4;
printf("%d", *a.x);
}
*a.x = 3;
printf("%d", *a.x);
}
return nRetCode;
}
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
It is not good, because objects of Singleton or Factory patterns can be defined in global space.
where can I read about what the variables is traced by Parallel Studio?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
The types of memory errors that can be detected by the Parallel Inspector are described in the Intel Parallel Inspector On-line Help.
As for the question regarding Singleton, it depends on how the Singleton was allocated. If it is allocated using malloc() or new() then we can detect the object leaking (even if the pointer to the object is in the global namespace). If it is allocated statically then there is no notion of the object leaking (static lifetime means that it is allocated when the program starts and is de-allocated when the program exits), and this is true whether the name of the object is visible globally or only inside a single file.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Essentially the leak detection tool is looking for the objects that were allocated dynamically, with malloc() or new(), and never deallocated with free() or delete(), that are no longer reachable through any chain of pointers originating at a statically allocated object.
In the example in the top message the global object of the A structure is still reachable from the static space - so it's not considered as a leak.- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// The one and only application object
CWinApp theApp;
namespace
{
/*
quote:
If it is allocated using malloc() or new() then we can detect the object leaking (even if the pointer to the object is in the global namespace).
*/
int* a; // memory leak was not detected
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
a = new int();//it is memory allocation for the pointer in the global namespace
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
_tprintf(_T("Fatal Error: MFC initialization failed\n"));
nRetCode = 1;
}
else
{
*a = 3;
int * p = new int();
*p = 4;
printf("%d%d\n", *a, *p);
}
return nRetCode;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I was wrong in my statement. I made more correct statement in the next message. So, since the memory allocated in the main can be reached by the global pointer a, it cannot be considered as a leak. Sorry for being not clear.
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page