0

My C++ code is written in Embarcadero 10.3.1. I am facing lot of memory leaks and resource leaks. I am unable to identify the leaks.

When I use CodeGaurd, the application freezes, so I'm unable to get any conclusions.

My application is a background job which continuously processes files and generates labels. It works fine for a couple of hours and generates around 3000 labels, and then goes to a hung/non-responsive state.

Can anyone suggest any solution?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Dhatri
  • 11
  • Is your application statically linked to the RTL? Try dynamic linking perhaps? – Totte Karlsson Aug 08 '19 at 04:52
  • 1. without MCVE nor at least some relevant code we can only guess ... 2. CodeGuard is unusable for big projects as its very memory and CPU hungry and for big projects what takes seconds in normal execution can take minutes or even crash with CodeGuard but if you cut down unnecessary stuff to manageable code size then CodeGuard is super. 3. background implies multithreading? 4. add wdebug prints/logs to your app so you can see what was the last known state before freeze... or what parts of code still run OK ... – Spektre Aug 08 '19 at 06:30
  • Also take a look at this: [traceback a pointer in c++ code gdb](https://stackoverflow.com/a/20565511/2521214) I created to solve my memory leaks in a big project leading to discovery and workaround of BCC compiler bug ... – Spektre Aug 08 '19 at 06:37
  • Is the label printing application really big? Small projects about 10-100K LOC don't have any problems with CodeGuard for me (however CG [doesn't work with CLang](https://www.arbinada.com/en/node/1602)). Can you decompose your app into smaller parts to test it separately? – serge Aug 09 '19 at 07:58
  • How did you realize the process has leaks? Via Task Manager? If it had leaks and finally no memory to allocate, the process would rather crash, but it become non responsive instead... BTW tried Deleaker? Recent versions support C++ Builder. – Artem Razin Aug 09 '19 at 12:33

1 Answers1

1

Memory leaks can be difficult to track down. In your case I suspect that you are using a label printer with it's own library or driver and leaks could be anywhere.

Firstly you should try and understand what memory management models exist in the application. With C++ Builder code generally you will be responsible for allocating and freeing memory. So every object you create with new should have a corresponding delete - make sure you understand what part of the code is responsible for freeing the object. (In 10.3.1 C++ Builder does support C++ auto_ptr but you may not be using this, and you can't guarantee that any library code you have linked in will honour the auto_ptr semantics).

If you are passing information into code that's using another memory management model (so using a COM Object is a good example) then make sure you understand the implications for memory management. If you pass it a pointer is it expecting to free it or is it expecting you to free it - and if it's you how do you know when it has finished with it.

Try running a smaller run and seeing if with a smaller run you can use CodeGuard and pick up anything it suggests.

If your system is in production you will want to keep it running. One option would be to run it as a Windows Scheduled Task. It will process a set number of files and exit. The O/S will free up resources it had in use (but not any that are being leaked at the system level, perhaps by a buggy driver). That may allow you to keep it running all day while you continue to find any leaks.

Good Luck!

Rob Lambden
  • 2,175
  • 6
  • 15
  • "*In 10.3.1 C++ Builder does support C++ `auto_ptr`*" - and for C++11 and later, it also supports `unique_ptr` and `shared_ptr`, too – Remy Lebeau Aug 08 '19 at 16:46