The application was unable to start correctly (or failed to initialize properly)

When one of the following error messages is shown after executing an application:

  • The application was unable to start correctly (0xc<7 hexadecinal digits>). Click OK to terminate the application.
  • The application failed to initialize properly (0xc<7 hexadecinal digits>). Click OK to terminate the application.

there is no information about what is the real problem. It may get more mystic if you are developing the application. You will try to debug it without success, since the same error will appear in Visual Studio, or any other IDE, and the first line of code in your main function is not reached. The application really can’t start.

This error usually means that a shared library (dll) is missing. If the missing shared library is a direct (first level) dependency, the error is different and informative (The program can’t start because XXXXXX.dll is missing from your computer). However, if a shared library calls another shared library, the former and obscure error will be displayed instead.

The solution: download the Dependency Walker application, and run it with your application executable to find the missing shared library file name. Once you know the file name, google it, find and download the prerequisite that contains the library, and install it.

Take into account that:

  1. The shared library version matters. If you install a different prerequisite version than the one originally used for compiling the calling shared library, the error may be still thrown. Therefore, you may try more than one version until you find the right one.
  2. Do not download single dll files from untrusted sites, since there is no guarantee that those files are untouched.

Real case scenario: your application uses some third-party shared libraries as black boxes (you don’t have the source code). Someone updates one of these third-party libraries at the common repository, and the updated library has a new dependency. When you update your local copy, compile and execute, you will find the error.

P.S.: In Spanish, the errors are translated as “La aplicación no se ha podido inicializar correctamente (0xc<7 dígitos hexadecimales>)” and “El programa no puede iniciarse porque falta XXXXXX.dll en el equipo”.

CMake and the executable stack size in Visual Studio

Today I have been struggling with my first mystical error at my new job. The C++ project was able to compile and execute. At least, the screen was showing the 3D initial menu. However, when I tried to open any of the sub-applications where big 3D models where used, strange exceptions were thrown, such as out of memory when loading a texture, vertex buffer can not be allocated, and similar ones depending on the sub-application executed.

I have been porting the project from a manually maintained .sln file to CMake. So, the first step was to make sure that the original executable was working, and it was. Then, I compared the compilation and linking flags in the both project files, and I removed the new flags, which were not present in the original solution, one by one. Finally, I discovered (after 5 hours) that CMake (version 2.8.10) adds to the linker flags /STACK:10000000, which means that the heap size is initialized to 10MB, instead of the default 1MB. Adding the following lines to the main CMake file:
STRING(REGEX REPLACE “/STACK:[0-9]+[ ]” “” CMAKE_EXE_LINKER_FLAGS “${CMAKE_EXE_LINKER_FLAGS}”)
SET(CMAKE_EXE_LINKER_FLAGS “${CMAKE_EXE_LINKER_FLAGS}”
CACHE STRING “Flags used by the linker.”
FORCE
)

solved the problem. Note that a similar problem may occur if there are multiple threads in the application, since the total amount of memory used will be num_threads by stack_size.

Note that this flag must be also removed from all your project dependencies that use CMake for generating project files. Then, the modified dependencies must be rebuild and finally your project, too.