Heap Corruption while using CreateWindowExW
As pointed out above, heap corruption is often detected after the real corruption has already occurred by some DLL/module loaded within your process. From your post it looks like this issue is windows platform specific so I would suggest you to use WinDBG/Pageheap and find out where actual memory corruption is happening. One very very good article about heap memory corruption analysis can be found from the book "Advanced Windows Debugging, Author: By: Mario Hewardt; Daniel Pravat" Chapter 06
http://advancedwindowsdebugging.com/ch06.pdf
Heap corruption when returning from function inside a dll
Most likely, you're seeing crashes due to the fact that, in Windows, DLLs have their own private heap.
When you compiled your function, the compiler generated some code for std::string
's destructor, to clean up its arguments. This code frees the allocated memory on the DLL heap. However, the application EXE also generates its own code for std::string
's constructor, which allocates the code on the program heap. When you allocate on one heap and free on the other, undefined behavior occurs, and you crash.
As for why small strings don't trigger the bug - many std::string
implementations inline small strings into the struct itself, to avoid heap overhead. When your string is small enough to fit, no memory allocation need take place, and thus it happens to appear to work... as long as you use the same STL version for both EXE and DLL, and the threshold for inlining never changes.
To avoid this issue, don't pass objects by value to DLLs (unless they are POD objects), and don't free an object in a different DLL or EXE than it was created in. Avoid passing STL or C++ library objects around as well, as their implementation may differ between different versions of the C++ compiler. Pass POD objects or C primitive types such as const char *
instead.
how to debug possible heap corruption?
I'd suggest you start by using Application Verifier to test for heap corruption. Application Verifier is a free tool from Microsoft that helps identify resource leaks and heap corruption. You can find App Verifier at url: http://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=20028 You can find out how to enable it for heap corruption and other things from http://technet.microsoft.com/en-us/library/bb457063.aspx
It is often useful in these situations.
Heap corruption during free()
The error message seems to be very clear that that it is heap corruption.
Try to move the free() for loop, above the function call k_means() and comment out the rest of your program.
I am suspicious that this is what corrupting the Heap!
If this experiment frees the memory correctly, You know the bug is in k_means() function...
Heap corruption on Windows but not Linux
As mentioned above that you do not get any exception related to heap corruption does not mean that it did not happen.There would be problem in your code and not in VS or compiler.My previous post on the similar article would be useful here as well.
https://stackoverflow.com/a/22074401/2724703
Probably you should also try to run some dynamic tool(Valgrind) on your linux. There is high possibility that, you would find the same bug using Valgrind as well.
These dynamic tools would give you the root cause of these problem.
System( pause ) Causes Heap Corruption
I believe the issue here is that there is security software running that is preventing certain DLL's from being loaded and/or injecting code to prevent malicious instructions from being executed. Since I've never experienced this on a personal machine and am only experiencing this on a corporate box that has a lot of security administration going on, I think it is that. I doubt it is a runtime mismatch, as nothing else produces unexpected results. Thank you everyone who commented with potential problems! Read the comments if you're having this issue, as there are other potential issues discussed there.
Heap corruption while trying to free a wchar_t pointer
Use
duplicate = static_cast(malloc(length * sizeof(wchar_t));
otherwise you do not hane enough space for the wide string
Qt heap memory corruption
In ExtWiiMote.h
you declared
QLabel* dots[3][3];
and in the ExtWiiMote.cpp
you use dots[3][0]...
.
Fix dots
array size and probably you'll be fine.
Related Topics
How to Pause a Pthread Any Time I Want
How to Use Std::Filesystem on Gcc 8
Errors When Linking to Protobuf 3 on Ms Visual C
How Would You Implement a Basic Event-Loop
Is a '=Default' Move Constructor Equivalent to a Member-Wise Move Constructor
Semicolon After Class Declaration Braces
C++11 Way to Index Tuple at Runtime Without Using Switch
How to Check If the Program Is Run from a Console
How to Alter a Float by Its Smallest Increment (Or Close to It)
Do I Need to Close a Std::Fstream
Loop Until Integer Input Is in Required Range Fails to Work with Non-Digit Character Inputs
Is Left-Shifting (<<) a Negative Integer Undefined Behavior in C++11
Compiling with G++ Using Multiple Cores
Differencebetween a Static and Const Variable