ifstream, floats, binary files, and release! oh my!

Started by
5 comments, last by Fruny 17 years, 6 months ago
I have this weird error that appears solely on Release build (MSVC 2005, C++). I have a class that represents an Object in the game which I load from a binary file. This shouldn't matter, but just to give you more information; this binary file is created from a GUI editor that is created using C#. When loading this data from the binary file I am using ifstream. This class currently only contains 1 float type and when loading from this binary file in debug mode works perfectly fine. However, while in release this value always gets set with 0. This is how I am doing it: fin.read( (char*)pActor->m_fAttackRadius, 4 ); Is there something that I am doing wrong or there perhaps a better way that I should be loading this from the binary file? tHom.
Advertisement
Does pActor->m_fAttackRadius have type float or type float* ?

If it's a float, you should use the address of operator eg:
fin.read( (char*)&pActor->m_fAttackRadius, 4 );

If it's a pointer to a float, you should make sure you allocated space for it.

HTH
First off, although I doubt it'll solve your problem, your line should probably read:

fin.read( (char*)&(pActor->mfAttackRadius), sizeof( pActor->mfAttackRadius ) )

Don't rely on a hard-coded size for a type like that (the size of the type may change on different platforms, and you might change the type yourself later on). You were also missing the &, but I'm guessing that was a typo.

The sizeof operator is evaluated at compile time, so using a hard-coded value doesn't even give you a speed increase.

Does the file containing the binary data actually get opened successfully? Your working directory may be set differently in Release, and thus the application may be unable to find the file where it was looking in Debug.

Are there any features in your program that you only have turned on in debug mode? Could those affect deserialization?

The variable is a float (not a float*), and the line should have the ampersand (yes it was a typo, as I was reading it from the laptop which was having problems connecting to the wireless :( ). I will try using the sizeof() operator in a few minutes and hope that it works.

Thanks for the help so far!

tHom

EDIT: The line now looks like this, but the float is still set to zero after this line is executed (checked via logger and the results from the game yeild that it is still 0):

fin.read( (char*)&(pActor->m_fAttackRadius), sizeof( pActor->m_fAttackRadius ) );

[Edited by - tHomahwk on October 18, 2006 11:10:41 AM]
Quote:Original post by tHomahwk
This class currently only contains 1 float type and when loading from this binary file in debug mode works perfectly fine. However, while in release this value always gets set with 0.


Beware that the debug and release executables are in different directories. Where is the file you are reading from?
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Quite true fruny, but I would suspect the OP checks (or should do) the file is open/valid.

typo in mod name oops.
In my time here, I have learned never to assume anything. [rolleyes]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement