Mam dylemat, bo moja wiedza co do pamięci alokowanej dynamicznie jest trochę ograniczona i zastanawiam się nad dwoma sposobami zwolnienia jej w pewnym przykładzie (Są to dwie metody relizujące wspólnie konwersję łańcuchów znakowych z wchar_t* do char*). Który sposób jest bardziej poprawny i nie spowoduje wycieku?? (Myślałem też o inteligentnych wskaźnikach, ale na razie chcę to rozwiązać w ten sposób ;) ) [Wybaczcie wstawki z angielskiego ;] ]
I sposób:
char* km_Common::getSingleString(const wchar_t* wString) throw()
{
char* retString;
try {
retString = protGetSingleString(wString);
} catch (const km_ErrorRuntime& e) {
//todo: inform about an error here
//MessageBoxA(NULL, e.what(), "Error", MB_OK);
return "";
}
return retString;
}
char* km_Common::protGetSingleString(const wchar_t* wString) throw(km_ErrorStringConvert)
{
//! Obtain size of the string, MSDN ::WideCharToMultiByte
int iSize = ::WideCharToMultiByte(CP_ACP, 0, wString, static_cast<int>(wcslen(wString)), NULL, 0, NULL, NULL);
if (iSize <= 0)
{
//throw exception
throw km_ErrorStringConvert("Error while converting from wchar_t* to char*. Unknown size!");
}
char* cString;
try {
cString = new char [iSize];
} catch (const std::bad_alloc &e) {
//memory couldnt be allocated
return "";
}
//! Convert the string
int ret = ::WideCharToMultiByte(CP_ACP, 0, wString, static_cast<int>(wcslen(wString)), cString, iSize, NULL, NULL);
if (ret == 0)
{
//throw exception
delete[] cString;
throw km_ErrorStringConvert("Error while converting from wchar_t* to char*. Operation failed!");
}
return cString; // czy tu nastąpi wyciek? wskaźnik ze stosu jest niszczony, ale co ze stertą?
}
II sposób:
char* km_Common::getSingleString(const wchar_t* wString) throw()
{
char* retString;
char* cString = NULL;
try {
retString = protGetSingleString(wString, cString);
} catch (const km_ErrorRuntime& e) {
//todo: inform about an error here
//MessageBoxA(NULL, e.what(), "Error", MB_OK);
delete[] cString; //zwolnienie pamieci zaalokowanej w protGetSingleString(), tylko co sie stanie w momencie wyrzucenia w protGetSingleString wyjątku std::bad_alloc ???
return "";
}
delete[] cString; // j/w
return retString;
}
char* km_Common::protGetSingleString(const wchar_t* wString, char* cString) throw(km_ErrorStringConvert)
{
//! Obtain size of the string
int iSize = ::WideCharToMultiByte(CP_ACP, 0, wString, static_cast<int>(wcslen(wString)), NULL, 0, NULL, NULL);
if (iSize <= 0)
{
//throw exception
throw km_ErrorStringConvert("Error while converting from wchar_t* to char*. Unknown size!");
}
try {
cString = new char [iSize];
} catch (const std::bad_alloc &e) {
//memory couldnt be allocated
return "";
}
//! Convert the string
int ret = ::WideCharToMultiByte(CP_ACP, 0, wString, static_cast<int>(wcslen(wString)), cString, iSize, NULL, NULL);
if (ret == 0)
{
//throw exception
delete[] cString;
throw km_ErrorStringConvert("Error while converting from wchar_t* to char*. Operation failed!");
}
return cString;
}
Z góry dzięki za pomoc ;)
P.S. Oba przykłady kompilują się i "niby" działają poprawnie.