0

Recently I have implemented a complete class with its own member methods as well non-member methods related to that class in Visual C++ 2015. I have written unit tests (C++ Unit Test) for that class, but the problem I am countering now is how can I check for memory leaks when executing the unit tests?

I have heard a lot of different alternatives to do this, but these alternatives commonly are only to test a Concrete implementation, like a main routine or something like that. That is the impression I have gained after Reading several articles about performing memory leak detection in Visual C++. In this case, I would like to check for memory leaks among the unit tests I have written, so what is the most "comfortable" way to do this?

  • 2
    Make sure you never say `new` or `delete`, and you shouldn't have trouble with leaks. – Kerrek SB Dec 11 '16 at 16:15
  • The problem is that I actually do that, especially when I am creating pointers at several locations among my codes. – user2566415 Dec 11 '16 at 16:16
  • 2
    @user2566415 use shared/unique pointers instead – W.F. Dec 11 '16 at 16:17
  • Problem is that I am working on an programming assignment, where you are limited to use pointers when representing an array and I have a lot of methods which performs changes and other manipulations on that array being represented by some pointer. So that is why I am asking for some good memory leak detection tools in combination with running unit tests. – user2566415 Dec 11 '16 at 16:20
  • @user2566415 in this case the teacher who assigned you the task should answer this question better :) – W.F. Dec 11 '16 at 16:28
  • But seriously, doesn't Visual Studio have support for this stuff? Now I would like to know this for myself, because there are so many articles about memory detection without taking unit testing in consideration. Could you or someone else give links that are relevant to my problem so I can read more on this? – user2566415 Dec 11 '16 at 16:37
  • 3
    @user2566415 [this question](http://stackoverflow.com/questions/413477/is-there-a-good-valgrind-substitute-for-windows) points to e.g. [Visual Leak Detector](http://vld.codeplex.com/) maybe it'll help – W.F. Dec 11 '16 at 16:53
  • I'll try that and return with answer later. Thanks for the tip! – user2566415 Dec 11 '16 at 16:58
  • No, VSL did not work out well for me. I could hardly run my tests and nothing special was outputted regarding possible memory leaks (I even started to receive strange error messages about several files being corrupted). Anyway, are there other options? – user2566415 Dec 11 '16 at 17:54
  • http://libmss.sourceforge.net/ Quite old, but may help you. – Ripi2 Dec 11 '16 at 18:08
  • @Ripi2 How do I use that in Visual Studio? – user2566415 Dec 11 '16 at 18:27
  • Read the doc in the web site. You have to compile MSS, add the lib to your linker path and #include a file on every file you use. – Ripi2 Dec 11 '16 at 18:43
  • Okay, I give this a shot. I return later with answer. – user2566415 Dec 11 '16 at 18:45
  • If you are using a debug build, you could try [_CrtDumpMemoryLeaks](https://msdn.microsoft.com/en-us/library/d41t22sb.aspx). – ssbssa Dec 12 '16 at 16:46
  • What is VSL, @user2566415 ? – sergiol Dec 12 '16 at 17:22
  • @sergiol, sorry, that was a typo. I mean VLD (=Visual Leak Detector). Btw, I made it work with Visual Leak Detector in Visual Studio for C++ programming, follow the guides PROPERLY in this [link](https://codeyarns.com/2010/09/11/visual-leak-detector-investigate-memory-leaks-in-visual-c/) and it should work fine for you. Thanks for all help/advices I have gotten here! – user2566415 Dec 14 '16 at 15:59

0 Answers0