Bug with UNICODE and std::vector?
Hello all,
Im not sure, but I get some pretty strange results here.
When I run this program debug compiled but NOT in debug mode, or release
compiled
I get an exception, sometimes immediately, sometimes after some time.
If I run in debug mode, everything runs fine.
I THINK it has something to do with reallocating of the vector if its
capacity is too small.
If I replace all UNICODE stuff with "normal" characters, I don't have any
problem.
anyone the same experience? Or am I doing something wrong?
Bas from Holland
excerpt from memory.h:
void deallocate(pointer _Ptr, size_type)
{ // deallocate object at _Ptr, ignore size
::operator delete(_Ptr); <---- here I get an
exception.
}
the program:
#include <iostream>
#include <vector>
#include <string>
class Basis {
public:
Basis* pLeft; // I dont use, these pointers were also in the original
program where I got this exception.
Basis* pRight;
};
class Test1 : public Basis {
public:
wchar_t cChar;
Test1(wchar_t c = L'\0') : cChar(c) {}
};
int main(int argc, char* charv[])
{
std::vector<Basis*> vec;
char buf[150];
std::string str;
std::wstring wstr;
do {
memset(buf,0,150);
std::cout << "\nEnter: ";
std::getline(std::cin,str);
// I use this to convert the "normal" string in a UNICODE string
size_t iConverted;
int strLengte = str.length();
wchar_t* pBuf = new wchar_t(strLengte +2);
memset(pBuf,0,strLengte + 3 );
mbstowcs_s(&iConverted,pBuf,str.length()+1,str.c_str(),_TRUNCATE);
wstr= std::wstring(pBuf);
for (std::string::iterator it = str.begin(); it != str.end(); it++)
{
vec.push_back(new Test1(*it));
}
for (std::vector<Basis*>::iterator it = vec.begin(); it !=
vec.end(); it++)
std::cout << (char)((Test1*)(*it))->cChar;
std::cout << "\n";
vec.erase(vec.begin(),vec.end());
} while (wstr != L"stop");
return 0;
}