maby i found the problem

To the Pros out there:

Hy, im searching for a bug since 2 days, and i asked a lot of pros in my company, but nobody could help me.
Heres the situation:

Visual Studio Microsoft Visual Studio 2005 Version 8.0.50727.762 (SP.050727-7600)

I had a project with a strange bug, so i could melt down the problem into this simple program (I took the original Project and

deleted file after file to look when it would go correctly) and still got the strange behaviour.

heres the whole program, its a .vcproj-file project with 1 source file called main.cpp where the following is inside:

#include <string>

class EventTest
{
public:

EventTest (int a_id, const std:Tongue Tiedtring& a_text);

int m_Id;
std:Tongue Tiedtring m_Text;
};

/*inline*/ EventTest::EventTest (int a_id, const std:Tongue Tiedtring& a_text)
: m_Id(a_id)
, m_Text(a_text)

{
//m_Id = a_id;
//m_Text = a_text;
}

int main(int argc, char** argv)
{
EventTest test(1, "Text");
//std:Tongue Tiedtring strTest = "TEST";

return 0;
}

there are also some additional Dependencies in the Properties, but i wont mention it here.

The result when debugging it is extremely strange. its about the content of m_Text who is (watched with debugger) always

something like "0,0,0,0,T,e,x,t,-b,4,0,0,0,d,4,....." (and signs i cant write here) and so on with a string size of _MySize =
14220893!! instead of 4.

As soon as i use a variable of type std:Tongue Tiedtring (the line that is commented now) all works fine!!!
I can make the behaviour the other way round as well: make the constructor inline, or initialize the members in the body. when doing either of the two, then the program works only correct (having "Text" in the string) when NOT having the variable of type std:Tongue Tiedtring in the code!!

I made an other new project (not deleting things from the original project) with exactly the same code, the same project
settings, and the same Dependencies. And this program is ALWAYS working correctly!!! but i cant find any difference, its like magic.also the output (which dlls are loaded) is the same.

The program would acctually work finde only this std:Tongue Tiedtring in the class is nasty!

Does anybody has an idea? or a tipp how to analyze the problem further?

Im sure it is some strange sideeffect, but of what?

thanx for any input!

andreas

[3006 byte] By [Andruu75] at [2008-1-10]
# 1
Well...the code looks okay...and I don't see why the string would not be populated correctly. Now...to get the debugger and its display out of the picture...what happens if you print out the text to a console for example?
AndreasMasur at 2007-10-3 > top of Msdn Tech,Visual C++,Visual C++ General...
# 2
Hello, thanx for your answer, was a good idea. i printed the string into a file:

FILE* f = fopen("c:\\TestFile.txt", "wb+");

if (f != NULL)
{
fwrite(test.m_Text.c_str(), 1, 200, f);
fclose(f);
}

but the file was full with strange signs "Text ù? °1ù °1ù   °? \ @  @  ú? jù è@ù Z@ ?ù?  °1" and so on...
i also tried it in release version, but the same thing happened.

how can it be that a std:Tongue Tiedtring acts so weird? is there a wrong dll loaded? i cant imagine, because i checked exactly what the program is loading at the beginning, and that looks ok.

greets andrduu

Andruu75 at 2007-10-3 > top of Msdn Tech,Visual C++,Visual C++ General...
# 3
Well you're writing 200 chars with fwrite, and that won't stop until all 200 chars are written. That is very likely to include some seemingly random data from the memory following your string. Use the <fstream> header, create an ofstream and pipe the contents of the string to that. I bet you'd see a lot less wacky data Smile
einaros at 2007-10-3 > top of Msdn Tech,Visual C++,Visual C++ General...
# 4

Are you sure your project is not set for Unicode? I do not understand how that could produce the results you are getting, but since there is not a more reasonable explanation the methodical diagnostic technique includes verifying assumptions. Check the headers and the project settings (properties) for relevant definitions too.

Also check everywhere for #pragma pack. Again, I don't know how that could cause this and not cause additional problems but it is easy to check for.

SimpleSamples at 2007-10-3 > top of Msdn Tech,Visual C++,Visual C++ General...
# 5
Andruu,

Einar already pointed out why you are seeing the weird characters. In addition, there is no need for binary mode. Looking at the output, I would think that the string itself is fine and you are rather looking at a debugger display issue (Debugger was never that great in working with STL classes anyway).

As pointed out...use the standard file stream class:

Code Block

#include <string>
#include <fstream>

class EventTest
{
public:
EventTest (int a_id, const std::string& a_text);

int m_Id;
std::string m_Text;
};


inline EventTest::EventTest (int a_id, const std::string& a_text)
: m_Id(a_Id),
m_Text(a_text) {}

int main(int argc, char** argv)
{
EventTest test(1, "Text");

std::ofstream ofs("c:\\TestFile.txt");

if(ofs)

ofs << test.m_Text;

return 0;

}


AndreasMasur at 2007-10-3 > top of Msdn Tech,Visual C++,Visual C++ General...
# 6
1. thanx for the tip with the std:Surprisefstream, the problem is, i cant test if you are right, because as soon as i write these 3 lines of code:

std:Surprisefstream ofs("c:\\TestFile.txt");
if(ofs)
ofs << test.m_Text;

after the initialisation, everything works fine, the effect has gone! i see the right text in the variable and therefore also in the
file! very nasty..

2i just realised, that when i delete the file vc80.idb (minimum rebuild dependency File)
wich is in the same folder as the .vcproj file, the thing is working correctly!! in all my experiments i always copied the whole project and changed it, so i also copied the vc80.idb, wich is not deleted after a rebuild all.

But i also found out, that the problem can occur again, and then i have to delete the file again.

any clue what the reason can be?

Andruu75 at 2007-10-3 > top of Msdn Tech,Visual C++,Visual C++ General...
# 7
The string class is the same whether you build for unicode or not, so that's not the case here.

Also, what are your actual problems now? You can't build the application? "if (ofs)" won't actually do what you expect (it won't do anything at all), but other than that your use of the fstream is fine, so I expect it to output proper data. If that's not the case, please show us what happens. If you're getting any errors, please show those too.

einaros at 2007-10-3 > top of Msdn Tech,Visual C++,Visual C++ General...
# 8
einaros wrote:
"if (ofs)" won't actually do what you expect (it won't do anything at all), but other than that your use of the fstream is fine, so I expect it to output proper data.

Actually that is not true...the line checks whether a failure happened on the stream while opening it...
AndreasMasur at 2007-10-3 > top of Msdn Tech,Visual C++,Visual C++ General...
# 9
Yeah, you're right, and frankly I'm surprised. I don't find it good practice at all to allow that, no matter how practical it may be to have a shorthand.
einaros at 2007-10-3 > top of Msdn Tech,Visual C++,Visual C++ General...
# 10
einaros wrote:
Yeah, you're right, and frankly I'm surprised. I don't find it good practice at all to allow that, no matter how practical it may be to have a shorthand.

Einar,

I certainly agree that it looks confusing since you rather expects something like this while using pointers for example. In addition, this can actually lead to obscure errors while forgetting about the operator precedence. Most of the time, calling 'fail()' is a more readable solution...I think I just got lazy while writing...

AndreasMasur at 2007-10-3 > top of Msdn Tech,Visual C++,Visual C++ General...
# 11
It's in the standard, so it cannot be blamed on any particular library vendor. Nevertheless I find is_open more clear after calls to open (and ditto for the ctor), and the various other status helpers otherwise.

I think I'll file it under "what were they thinking?", and move on.

einaros at 2007-10-3 > top of Msdn Tech,Visual C++,Visual C++ General...
# 12
I have seen exactly the same problem in Visual Studio 2008. This was not present in 2003, where strings were displayed as you would think. I suspect its a bug in the development studio.
SJM at 2008-1-16 > top of Msdn Tech,Visual C++,Visual C++ General...