Reputation: 304
I want to call an external program to convert BMP to DDS files, but after a few calls it crashes all the time. I tried both ShellExecute
and CreateProcessor
. Here the example with ShellExecute
:
path = "C:\\pictures";
file = "C:\\pictures\\test.bmp";
string cmd = "-f BC1_UNORM -o " + path + " " + file;
char* cmdConvert= new char[cmd.size()];
strcpy(cmdConvert, cmd.c_str());
int buffSize = (int)strlen(cmdConvert) + 1;
LPWSTR cmdL= new wchar_t[buffSize];
MultiByteToWideChar(CP_ACP, 0, cmdConvert, buffSize, cmdL, buffSize);
SHELLEXECUTEINFO ShExecInfo = {0};
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShEecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = NULL;
ShExecInfo.lpFile = _T("C:\\Texconv\\texconv.exe");
ShExecInfo.lpParameters = cmdL;
ShExecInfo.lpDirectory = NULL;
ShExecInfo.nShow = SW_HIDE;
ShExecInfo.hInstApp = NULL;
ShellExecuteEx(&ShExecInfo);
WaitForSingleObject(ShExecInfo.hProcess,INFINITE);
CloseHandle(ShExecInfo.hProcess);
delete convertMe;
delete gah;
It crashes directly after ShellExecuteEx(&ShExecInfo)
. There must be some race conditions (or something like this), because it is not crashing when running in the debugger (I'm using VS2012).
Upvotes: 3
Views: 288
Reputation: 4557
You are missing a terminal zero in line 4:
char* cmdConvert= new char[cmd.size()];
write:
char* cmdConvert= new char[cmd.size()+1];
As, what I think you already now, empty string buffer is one byte long because of the terminal zero character. A cmd.size() does not include this terminal character. For a sring buffer you had to add one byte to the string length. The crash in release build is a result of overwriting something after that buffer. In debug mode the new operator pads some byte in the beginning and end of allocated buffer to support buffer overwrite detection, that's the reason it runs in debug.
Upvotes: 2