Link to home
Start Free TrialLog in
Avatar of Mensana
Mensana

asked on

Strange behaviour for a Unicode project

Hi everybody,
I’ve got this Unicode project that deals with some ASCII and Unicode text files. It used to work fine but all of a sudden it started to have a strange behaviour: It’s doing what it is supposed to do when I run it in debug mode but it’s not working the same way if I try to run the generated “.exe” even though, of course, I’m not changing anything. I’ve tried to rebuild the whole project several times but I still get the same result. The piece of code that is working different involves a Unicode text file and it looks like:

// ...

// try to open the file
CFile *pFile = NULL;
pFile=new CFile(strFilePathName,
                CFile::modeReadWrite);

// get the file's size
UINT nFileLength=0;
nfileLength=(UINT)pFile->GetLength();

// allocate space for the buffer
// pBuffer is declared like
// “_TCHAR *pBuffer”
pBuffer=new _TCHAR[nFileLength /
                   sizeof(_TCHAR)];

// read the file
pFile->Read(pBuffer, nFileLength);

// construct a CString object that
// mirrors the file
CString strFile = pBuffer;

// ...

Now, for some reasons, when I run in debug mode, the string strFile gets set with the actual content of the file whereas when I run the released “.exe”, the same string is empty. Does anybody has any clue on this one?

TIA,
Eddie.
Avatar of jkr
jkr
Flag of Germany image

One thing:

pBuffer=new _TCHAR[nFileLength /
                   sizeof(_TCHAR)];

should read

pBuffer=new _TCHAR[nFileLength * // !!!
                   sizeof(_TCHAR)];
ASKER CERTIFIED SOLUTION
Avatar of chensu
chensu
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mensana
Mensana

ASKER

For jkr:

I don’t really think so!

The CFile::GetLength() returns the file’s length in bytes and not in number of _TCHARs. To get the number of _TCHARs (used to allocate the buffer) you have to divide the length of the file by the size of a _TCHAR.

Sorry, but you’re wrong.
Eddie.

Avatar of Mensana

ASKER

Well, thank you chensu!

You were right. By adding the null-terminator character everything started to work again. Now, if you have a little spare time, maybe you’ll explain how come my project worked fine in debug and didn’t work in release. What’s the difference?

Thanks again,
Eddie.

Avatar of Mensana

ASKER

For chensu:

I have an observation to make. The null-terminator should be added at a different offset.
Because the CFile::Read function reads bytes and not TCHARs the dimension of my buffer will be nFileLength / sizeof(_TCHAR) instead of nFileLength. Having this said the code should look like:

pBuffer=new _TCHAR[nFileLength / sizeof(_TCHAR) + 1];

// read the file
pFile->Read(pBuffer, nFileLength);

// Don't forget the null-terminator
pBuffer[nFileLength / sizeof(_TCHAR)] = _T('\0');

// construct a CString object that
// mirrors the file
CString strFile = pBuffer;


Thanks,
Eddie.
>how come my project worked fine in debug and didn’t work in release. What’s the difference?

The debug mode normally allocates a little more memory than requested. And it happened the end of the buffer was zero. This kind of bugs very much depends on the current status of the system. It seems to work fine sometimes.

>pBuffer[nFileLength / sizeof(_TCHAR)] = _T('\0');

That's absolutely right.