Software Archive
Read-only legacy content

It is an bug?

sg_2
Beginner
1,350 Views
I have downloaded trial version of the Parallel Studio and created the test console project with MFC using VS2008 wizard. The code is shown below and comments describe a problem :

#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;
}

0 Kudos
6 Replies
Vladimir_T_Intel
Moderator
1,350 Views
Hi,
This is not considered as a bug. Inspector doesn't report leaks of the objects created in the global space.
0 Kudos
sg_2
Beginner
1,350 Views
Hi,

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?
0 Kudos
Vladimir_T_Intel
Moderator
1,350 Views

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.

0 Kudos
Vladimir_T_Intel
Moderator
1,350 Views

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.
0 Kudos
sg_2
Beginner
1,350 Views

#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;
}

0 Kudos
Vladimir_T_Intel
Moderator
1,350 Views

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.

0 Kudos
Reply