I am facing memory corruption and crash when doing a string tokenize. The crash is not occuring in the first time of processing . It takes more than an hour to crash. The problemmatic
wchar_t *query = new wchar_t[inQry.length() + 1];
memset(query, NULL, inQry.length() + 1);
memcpy(query, inQry.c_str(), inQry.size());
wchar_t *Tok = wcstok(query, L" ");
headStr = L"";
tableName = L"QUERY";
while(Tok != NULL)
{
vectorSQLEntry.push_back(Tok);
Tok = wcstok(NULL, L" ");
}
int tokCount = vectorSQLEntry.size();
if(query != NULL)
{
delete query;
}
When diagonized with debugdiag and analyzing the dump created, it pointed out some other line in the code.(sometimes the line will be delete query
).
So i removed the query
as pointer and declared as wstring. And i removed the
initialization of headStr
and tableName
inbetween wcstok and while loop(code as below).
wstring tmpQuery = inQry;
wchar_t *Tok = wcstok((wchar_t*)tmpQuery.c_str(), L" ");
while(Tok != NULL)
{
vectorSQLEntry.push_back(Tok);
Tok = wcstok(NULL, L" ");
}
With this code the crash is not occurring.! So what is the problem with the initial code? This crash dragged me more than two days.