2

I have a rather large project I'm working on in C++/Win32, and now that I'm nearing completion, I wanted to start testing it on other machines. My project works great in Visual Studio - either in Debug or Release mode. However, if I try to run either executable from its build directory (with all of the supporting files in place) they crash. I tried using the debug feature provided by visual studio, and it opened malloc.c, saying that a heap has been corrupted. If I had a memory leak or something somewhere, why wouldn't I be getting this error when I run my project from Visual Studio? I could use some pointers on how to debug what the problem might be.

Brian Gradin
  • 2,165
  • 1
  • 21
  • 42
  • are you linking to external libraries? – Lefteris E May 12 '13 at 06:13
  • How? There are 3 library files included in my project but I assumed they would automatically build into the executable – Brian Gradin May 12 '13 at 06:36
  • what are those libraries? – Lefteris E May 12 '13 at 07:35
  • Actually it turns out it only has one that I added - htmlhelp.lib – Brian Gradin May 12 '13 at 07:37
  • What do you have under project->properties->C/C++->Code Generation->Runtime Library? – Lefteris E May 12 '13 at 07:42
  • Multi-threaded debug DLL – Brian Gradin May 12 '13 at 07:51
  • Try the /MT or /MTd option, and rebuild the project. Some times the library heap type differs from the applications heap. And thus causes heap corruption. i don't know what htmlhelp was compiled under tho. – Lefteris E May 12 '13 at 07:58
  • I finally got it to run! But here's the thing: 1) Only the release-mode .exe ever works 2) That .exe has to be run once (and crash) before it can run successfully 3) I got it to work by running and letting it crash, then debugging with Visual Studio and finding out that it was caused by the use of the mbstowcs_s function in my code. I made the 3rd parameter one smaller and made the size of the array referenced by the second parameter one bigger. There are still other instances of the mbstowcs_s function elsewhere in my code which I couldn't fix this easily. Any idea what's happening? – Brian Gradin May 12 '13 at 08:14
  • @BrianGradin You should update your question with this info. – Mikhail May 12 '13 at 11:34

1 Answers1

2

I don't think that is due to memory leak. It has happened to me too when I tried to copy only the compiled executable but not depend libraries. So just check whether all depend libraries are available in other systems too.

Sanoob
  • 2,466
  • 4
  • 30
  • 38
  • The thing is I didn't even copy it anywhere - I just ran it by double clicking the executable rather than from visual studio. I do have 3 library files linked to in my project. Is there a certain way I have to link them to the executable? – Brian Gradin May 12 '13 at 06:37
  • There are many way to link lib. 1) Putting all lib in same directory. 2) Putt it in system32 folder. 3) Add the library path to environment PATH variable. 4) Reg command to register your *.dll lib files – Sanoob May 12 '13 at 07:08
  • Actually as it turns out, my project only has one library dependency. I put it in System32 and nothing changed. – Brian Gradin May 12 '13 at 07:19
  • How do I make sure the .lib file is included in the final build? – Brian Gradin May 12 '13 at 07:31
  • Ok, well then it must be something else. Release mode has the same problems debug mode has. – Brian Gradin May 12 '13 at 07:41
  • What is the liker and compiler options you have specified ? – Sanoob May 12 '13 at 07:45
  • They are all the defaults except I added htmlhelp.lib, winmm.lib and shell32.lib to the additional dependencies. – Brian Gradin May 12 '13 at 07:52
  • Still I couldn’t figure out what is going wrong. But I am sure that it is because of your compiler or liker. Do a Clean Build And again build the project with compiler option /MD . And avoid all unnecessary libraries. If not fixed try to find which library causing that removing additional lib. Or try using other compilers – Sanoob May 12 '13 at 08:19
  • I finally got it to run! But here's the thing: 1) Only the release-mode .exe ever works 2) That .exe has to be run once (and crash) before it can run successfully 3) I got it to work by running and letting it crash, then debugging with Visual Studio and finding out that it was caused by the use of the mbstowcs_s function in my code. I made the 3rd parameter one smaller and made the size of the array referenced by the second parameter one bigger. There are still other instances of the mbstowcs_s function elsewhere in my code which I couldn't fix this easily. Any idea what's happening? – Brian Gradin May 12 '13 at 08:20
  • 2
    @BrianGradin it's difficult to say without seeing sample code, but it sounds like you are passing in a destination buffer size into [mbstowcs_s](http://msdn.microsoft.com/en-us/library/eyktyxsx%28v=vs.80%29.aspx) that is larger than the actual buffer size. The function is then corrupting the heap and causing your crash. Running the application in different ways moves memory around and it's down to luck as to whether you have a crash or not. Take a look at http://stackoverflow.com/questions/1010106/how-to-debug-heap-corruption-errors – Steve May 12 '13 at 11:27
  • You were right! I finally figured it out. I was passing in as the dest buffer size the size of the dest buffer, which was uninitialized. – Brian Gradin May 15 '13 at 05:14
  • Oh. Also, I carelessly forgot to delete some of the allocated memory. So there were memory leaks after all. – Brian Gradin May 15 '13 at 05:15