1. Memory leak. Check your code for those new and then NOT delete.
case UTF_8:
{
std::string s;
bResult = _ReadLine( s );
s += '\0';
int nLength = MultiByteToWideChar( CP_UTF8, 0, s.c_str(), -1
, NULL, 0 );
LPWSTR lpwStr = new WCHAR[nLength];
MultiByteToWideChar( CP_UTF8, 0, s.c_str(), -1, lpwStr, nLength );
nLength = WideCharToMultiByte( CP_ACP, 0, lpwStr, nLength, NULL, 0
, NULL, NULL );
LPSTR lpStr = new CHAR[nLength];
WideCharToMultiByte( CP_UTF8, 0, lpwStr, nLength, lpStr, nLength
, NULL, NULL );
s = lpStr;
}
break;
you could just use the macro codes as below
template <class T>
class ArrDelAdapter
{
public:
ArrDelAdapter( T* p ) : p_(p) {}
~ArrDelAdapter() { delete[] p_; }
private:
T* p_;
};
#define __ARRAY_NAME( n ) _anonymous##n
#define ARRAY_NAME( n ) __ARRAY_NAME( n )
#define AUTO_ARRAY( T, A ) std::auto_ptr<ArrDelAdapter<T> > \
ARRAY_NAME( __LINE__ )( new ArrDelAdapter<T>(A) )
#define __Variable( type, var, number ) type var##number
#define _Anonymous( type, number ) __Variable( type, _anonymous, number )
#define Anonymous( type ) _Anonymous( type, __LINE__ )
#define auto_array( T, Arr ) \
Anonymous( std::auto_ptr<ArrDelAdapter<T> > )( new ArrDelAdapter<T>(Arr) )
for example:
INT* A = new INT[ 1024 ];
auto_array( INT, A );
the array A will be deleted automatically
2. Reference suspension. Such as Johann Gerell's saying.
bool CTextFileIO::ReadLineW(LPCWSTR ws)
{
std::wstring s;
bool bResult=ReadLineW(s);
ws=s.c_str();
return bResult;
}
the resolvent is 4 ahead.
3. The logic is NOT precise enough. Such as the below codes, the bResult is NOT inessential and must be checked.
case UTF_8:
{
std::string s;
bResult = _ReadLine( s );
s += '\0';
int nLength = MultiByteToWideChar( CP_UTF8, 0, s.c_str(), -1
, NULL, 0 );
LPWSTR lpwStr = new WCHAR[nLength];
auto_array ( WCHAR, lpwStr );
MultiByteToWideChar( CP_UTF8, 0, s.c_str(), -1, lpwStr, nLength );
ws = lpwStr;
}
break;
4. The design is turbid. For example, the user method, such as
bool ReadLineA ( LPCSTR s );
is evilness.It should be explicitly as
bool ReadLineA( CHAR buf[], size_t& nBufsize );
bool CTextFileIO::ReadLineA ( CHAR s[], size_t& nBufsize )
{
std::string s_s;
bool bResult = ReadLineA( s_s );
if ( !bResult )
{
nBufsize = 0;
return false;
}
size_t nTmp ( s_s.length() );
if ( nBufsize <= nTmp )
{
nBufsize = nTmp +1;
return false;
}
strcpy_s ( s, nBufsize, s_s.c_str() );
nBufsize = nTmp + 1;
return bResult;
}
no any way, go any way.
modified on Monday, March 28, 2011 5:47 AM
|