<p>I have a .NET application, which is using an open source C++ compression library for compressing images.  We are accessing the C++ library via managed C++.  I'm seeing heap corruption during compression.  A call to _CrtIsValidHeapPointer is finding an error on a call to free() when cleaning up after compression.</p>
<p>Are there tools such as <a href="https://en.wikipedia.org/wiki/PurifyPlus" rel="nofollow noreferrer">Purify</a> to help diagnosis this problem and what is causing the heap corruption when working in a combination of managed and unmanaged code? I do have the exception caught in the debugger, but it would be nice to have other tools to help find the solution to the problem.</p>

[{'answer_id': 34982, 'author': 'Anders Sandvig', 'author_id': 1709, 'author_profile': 'https://Stackoverflow.com/users/1709', 'pm_score': 0, 'selected': False, 'text': '<p><a href="http://www-01.ibm.com/software/awdtools/purifyplus/win/" rel="nofollow noreferrer">Rational Purify for Windows</a> supports .NET, so I guess that could be used.</p>\n'}, {'answer_id': 34985, 'author': 'Misha M', 'author_id': 3467, 'author_profile': 'https://Stackoverflow.com/users/3467', 'pm_score': 1, 'selected': False, 'text': '<p>On <a href="https://en.wikipedia.org/wiki/Unix" rel="nofollow noreferrer">*nix</a>, there\'s a tool called <a href="http://valgrind.org/" rel="nofollow noreferrer">Valgrind</a> that I use for dealing with memory issues, like memory leaks and memory corruption.</p>\n'}, {'answer_id': 35176, 'author': 'Nathan', 'author_id': 3623, 'author_profile': 'https://Stackoverflow.com/users/3623', 'pm_score': 2, 'selected': True, 'text': '<p>In native code, if the corruption always occurs in the same place in memory, you can use a <a href="http://msdn.microsoft.com/en-us/library/350dyxd0.aspx" rel="nofollow noreferrer">data breakpoint</a> to break the debugger when that memory is changed. Unfortunately, you cannot set a data breakpoint in the managed C++ environment, presumably because the GC could move the object in memory.</p>\n\n<p>Not sure if this helps, but hopefully it leads you off in the right direction.</p>\n'}]
['https://Stackoverflow.com/questions/34973', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3429/']