0

I am trying to figure out a memory leak problem that we are having with our product. We are using VS2008 unmanaged C++ with CERN's Root 5.28/00b. I added the following to a header that I include in all files:

#ifndef __CINT__  
#define _CRTDBG_MAP_ALLOC  
#include <stdlib.h>  
#include <crtdbg.h>
#endif // __CINT__

I have also added the following inside the main.cpp which includes the header which is included in all files:

int prevState = _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);

This causes _CrtDumpMemoryLeaks() to properly execute on exit. I have also taken to explicitly calling _CrtDumpMemoryLeaks() when a particular process within the application has completed, since this is where I am suspecting memory leaks. I am getting a dump of "memory leaks", but it doesn't have the file path and line number information that would make the dump much more valuable to me. Many of the dump entries seem to be considered leaks because the application calls exit(0) without first calling the associated destructors. It is impossible to tell how many are actually leaks. With the file name and line number information I would be able to tell more accurately whether something is a leak, or just the cause of the application not destroying it at exit.

Before you comment on these not being destroyed, the code has a lot more cleanup to do other than that, but we are trying to get at the more immediate issues first.

My problem is that when I add the following lines to my code, which I have seen as a suggested fix, I cannot build:

#ifdef _DEBUG
#define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
#define new DEBUG_NEW
#endif

I get the build error:

error C2661: 'TObject::operator new' : no overloaded function takes 4 arguments

It seems that there is a conflict between the overload of new that both Root and the CRTDBG definition does. If anyone has a word of wisdom that would help me to get passed this conflict, so that I could start getting file path and line number information, I would very much appreciate it. Thanks for your help.

bmahf
  • 965
  • 2
  • 11
  • 27
  • You can use the [`push_macro`](https://learn.microsoft.com/en-us/cpp/preprocessor/push-macro?view=msvc-160) and pop_macro pragmas around your call to TObject's new. – 1201ProgramAlarm Mar 31 '21 at 20:12
  • Related [question](https://stackoverflow.com/questions/10952731/error-c2661-cobjectoperator-new-no-overloaded-function-takes-4-arguments). – 1201ProgramAlarm Mar 31 '21 at 20:17
  • Thank you both. I will take a look at the push/pop_macro, and the undef/def. There will, unfortunately, end up being a lot of such changes in order to comprehensively cover the possible issues, so it'll be a bit before I'm back with my results. – bmahf Apr 01 '21 at 19:21

0 Answers0