C ++ OS dependent memory leaks?

CONTEXT

I am running Valgrind on my Linux codebase for my cross-platform library. I am trying to check if this is enough, or if I have to run dynamic code analysis on Windows and Mac too

Question

If my independent platform C ++ code does not flow on Linux ( Valgrind ), can we assume that this is not a leak on Windows and Mac too? If not, please provide the platform with independent C ++, the sample does not leak on Linux (according to Valgrind ), but leaks on Windows and / or Mac (select "ordinary" compilers, like those in VC ++, GCC, etc. .).

Accuracy (thanks to comments and answers)

  • I'm interested in platform-independent C ++ code (since there is no #ifdef, etc.);
  • I believe that I have C ++ code, not third-party code;
  • I consider Walgrind to be true, but I could consider any other tool. I know that no tool can detect all memory leaks.
+4
source share
3 answers

You can be sure that the common code is not leaking, but of course, if you have a decent size application, it is likely that SOME of your code is specific to Linux, other bits specific to Windows and some parts specific to OS X.

Those parts that are not specific to Linux will of course not be validated by Valgrind.

So, if you have a piece of code that does:

 #if LINUX char buffer[512]; #else char buffer = new buffer[2048]; #endif ... use buffer ... 

then you have a memory leak on windows but not linux.

Clearly, this is a trivial example, but such things can occasionally infiltrate code.

And, of course, there is a chance that using a system call in one OS is β€œsafe”, so as not to close or otherwise β€œtell the OS that you are done”, and then a problem occurs in another OS.

In addition, as I mentioned earlier, Valgrind does not guarantee that you have no problem with memory usage - it detects things like:

  void func() { char *p = new [1700]; ... // no free of p; } 

or

  void func() { char *p = new [1700]; ... // No free. p = some_other_pointer; ... } 

but not:

  void func() { vector<int> v; for(;;) v.push_back(1); } 

because the memory is still β€œowned” by something. Of course, this specific example is pretty extreme, but you can have similar things where the code stores something and just adds more and more items to the store and never removes them.

+3
source

valgrind helps to find flaws, but does not guarantee correctness.

You can still have undefined behavior in your code and that undefined behavior can manifest itself differently on different platforms, including a memory leak on one and not the other.

+3
source

If you have conditionally compiled code (for example, #if defined (OS_LINUX) ), then you must surely make sure that its leak does not apply to every platform.

Note. This is not a complete answer, but only the case that I was thinking about.

+1
source

Source: https://habr.com/ru/post/1499571/


All Articles