VC++ 6.0 string bug ? (take a look plz)
-
Here the SIMPLE project demo to illustrate the strange bug I noticed just some minutes ago. http://digilander.iol.it/ilbanca/fuffa/Bug.zip Compile in Debug mode and press the button. Compile in Release and press the button. It beheaves in different ways (bad in Release) The function that made me crazy: [code] void Buggy( char* result ) { char s1[6]; char s2[5]; strcpy(s1,"abcde"); strcpy(s2,"BUG!"); // DOESNT WORK strcpy(result,"anystring"); strcat(result,s1); // THIS WAY WORKS // strcpy(result,s1); } [/code] In Debug mode works In Release mode not Setting 'Optimizations' to "Default" instead of "Maximixe Speed" solve the problem, but why? I want to append 's1' to 'result', and it appends 's2' :(((((((((( Bye.
-
Here the SIMPLE project demo to illustrate the strange bug I noticed just some minutes ago. http://digilander.iol.it/ilbanca/fuffa/Bug.zip Compile in Debug mode and press the button. Compile in Release and press the button. It beheaves in different ways (bad in Release) The function that made me crazy: [code] void Buggy( char* result ) { char s1[6]; char s2[5]; strcpy(s1,"abcde"); strcpy(s2,"BUG!"); // DOESNT WORK strcpy(result,"anystring"); strcat(result,s1); // THIS WAY WORKS // strcpy(result,s1); } [/code] In Debug mode works In Release mode not Setting 'Optimizations' to "Default" instead of "Maximixe Speed" solve the problem, but why? I want to append 's1' to 'result', and it appends 's2' :(((((((((( Bye.
What is 'result', coming in ? Has it been created, or just declared ? Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. "I'm thinking of getting married for companionship and so I have someone to cook and clean." - Martin Marvinski, 6/3/2002
-
What is 'result', coming in ? Has it been created, or just declared ? Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. "I'm thinking of getting married for companionship and so I have someone to cook and clean." - Martin Marvinski, 6/3/2002
-
void Buggy( char* result ) in the function declaration, where Buggy stores the result of the appending action.
Yes, but do you say char * pResult; , or do you say char * pResult = new char [255]; , for example ? Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. "I'm thinking of getting married for companionship and so I have someone to cook and clean." - Martin Marvinski, 6/3/2002
-
Yes, but do you say char * pResult; , or do you say char * pResult = new char [255]; , for example ? Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. "I'm thinking of getting married for companionship and so I have someone to cook and clean." - Martin Marvinski, 6/3/2002
As Christian suggests the definition of 'result' is most likely where the problem lie. Debug builds initialize the stack, heap etc. whereas release builds don't, which is one of the reasons they behave differently. Neville Franks, Author of ED for Windows. www.getsoft.com
-
Yes, but do you say char * pResult; , or do you say char * pResult = new char [255]; , for example ? Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. "I'm thinking of getting married for companionship and so I have someone to cook and clean." - Martin Marvinski, 6/3/2002
Yes, new char[255], is a string, vector of chars. I use Buggy() this way: void CBugDlg::OnButton1() { char* test = new char[255]; Buggy(test); m_sTest = test; delete [] test; UpdateData(FALSE); } or void CBugDlg::OnButton1() { char test[255]; Buggy(test); m_sTest = test; UpdateData(FALSE); } The bug vanishes if I turn Optimizations\MaximixeSpeed to Optimizations\Default. But this still sucks... I must know why :(
-
Here the SIMPLE project demo to illustrate the strange bug I noticed just some minutes ago. http://digilander.iol.it/ilbanca/fuffa/Bug.zip Compile in Debug mode and press the button. Compile in Release and press the button. It beheaves in different ways (bad in Release) The function that made me crazy: [code] void Buggy( char* result ) { char s1[6]; char s2[5]; strcpy(s1,"abcde"); strcpy(s2,"BUG!"); // DOESNT WORK strcpy(result,"anystring"); strcat(result,s1); // THIS WAY WORKS // strcpy(result,s1); } [/code] In Debug mode works In Release mode not Setting 'Optimizations' to "Default" instead of "Maximixe Speed" solve the problem, but why? I want to append 's1' to 'result', and it appends 's2' :(((((((((( Bye.
are you allocating memory for result??? Nish
My miniputt high is now 29 I do not think I can improve on that My temperament won't hold www.busterboy.org
-
are you allocating memory for result??? Nish
My miniputt high is now 29 I do not think I can improve on that My temperament won't hold www.busterboy.org
Yes... try the test project if you can, try Debug and Release, pressing the button: it gives no errors, but two different outputs :( http://digilander.iol.it/ilbanca/fuffa/Bug.zip
-
Yes, new char[255], is a string, vector of chars. I use Buggy() this way: void CBugDlg::OnButton1() { char* test = new char[255]; Buggy(test); m_sTest = test; delete [] test; UpdateData(FALSE); } or void CBugDlg::OnButton1() { char test[255]; Buggy(test); m_sTest = test; UpdateData(FALSE); } The bug vanishes if I turn Optimizations\MaximixeSpeed to Optimizations\Default. But this still sucks... I must know why :(
There is your problem - m_sText = test means both variables point to the same allocated data, which delete [] test will delete. You should also always set deleted pointers to NULL. Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. "I'm thinking of getting married for companionship and so I have someone to cook and clean." - Martin Marvinski, 6/3/2002
-
There is your problem - m_sText = test means both variables point to the same allocated data, which delete [] test will delete. You should also always set deleted pointers to NULL. Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. "I'm thinking of getting married for companionship and so I have someone to cook and clean." - Martin Marvinski, 6/3/2002
-
As Christian suggests the definition of 'result' is most likely where the problem lie. Debug builds initialize the stack, heap etc. whereas release builds don't, which is one of the reasons they behave differently. Neville Franks, Author of ED for Windows. www.getsoft.com
Ok. But this is an initialization: strcpy(result,"anystring"); Then: strcat(result,s1); should append 's1' to 'result', instead it appends the other string 's2' to 'result'... it has to do with Opt/MazimixeSpeed, that in Debug build is Disabled, but for a such simple task I don't figure out why causes this strange effect...
-
I am puzzled. It worked fine. Both in release and in debug Nish :confused:
My miniputt high is now 29 I do not think I can improve on that My temperament won't hold www.busterboy.org
-
Yes... try the test project if you can, try Debug and Release, pressing the button: it gives no errors, but two different outputs :( http://digilander.iol.it/ilbanca/fuffa/Bug.zip
I am puzzled. It worked fine. Both in release and in debug Nish :confused:
My miniputt high is now 29 I do not think I can improve on that My temperament won't hold www.busterboy.org
-
Here the SIMPLE project demo to illustrate the strange bug I noticed just some minutes ago. http://digilander.iol.it/ilbanca/fuffa/Bug.zip Compile in Debug mode and press the button. Compile in Release and press the button. It beheaves in different ways (bad in Release) The function that made me crazy: [code] void Buggy( char* result ) { char s1[6]; char s2[5]; strcpy(s1,"abcde"); strcpy(s2,"BUG!"); // DOESNT WORK strcpy(result,"anystring"); strcat(result,s1); // THIS WAY WORKS // strcpy(result,s1); } [/code] In Debug mode works In Release mode not Setting 'Optimizations' to "Default" instead of "Maximixe Speed" solve the problem, but why? I want to append 's1' to 'result', and it appends 's2' :(((((((((( Bye.
Memory in debug mode is buffered so that memory overflow conditions that can crash in release mode can work fine in debug mode. My first guess is that 'result' does not point to a block of memory of the size you are expecting. Are you sure you allocated sufficient memory for the trailing null character? result will need to point to at least 16 bytes of memory for this code to work properly ( "anything" = 9 s1 = 6 NULL char = 1.) "There's a slew of slip 'twixt cup and lip"
-
Memory in debug mode is buffered so that memory overflow conditions that can crash in release mode can work fine in debug mode. My first guess is that 'result' does not point to a block of memory of the size you are expecting. Are you sure you allocated sufficient memory for the trailing null character? result will need to point to at least 16 bytes of memory for this code to work properly ( "anything" = 9 s1 = 6 NULL char = 1.) "There's a slew of slip 'twixt cup and lip"
Yes it was large enough, 255 chars. The problem is that doesnt crash, but display a string contained in another char-vector, as if they did overlapped, I don't know. I thought of a NULL terminating that I could have missed so that the string 1 could extend to string 2 that was declared just after, but it's not the case. Turing Optimizations to Default fixes this bug on my machine, while in other pcs runs fine without altering this setting :(
-
Yes it was large enough, 255 chars. The problem is that doesnt crash, but display a string contained in another char-vector, as if they did overlapped, I don't know. I thought of a NULL terminating that I could have missed so that the string 1 could extend to string 2 that was declared just after, but it's not the case. Turing Optimizations to Default fixes this bug on my machine, while in other pcs runs fine without altering this setting :(
Sorry, I did not read the last line in your original post, so I misunderstood the nature of the bug. It sounds to me as though you have a damaged stack. The issue is *still* one of debug mode buffering memory more generously than release mode. You are going to need to trace back through the call stack to figure out where the problem is. Stack overflow bugs can cause all kinds of bizarre issue like this. Good hunting.:) "There's a slew of slip 'twixt cup and lip"
-
Frankesk wrote: Grrr.... I'm running WinXP, what your OS ? I ran it on XP [both debug and release] without any problems at all. Nish
My miniputt high is now 29 I do not think I can improve on that My temperament won't hold www.busterboy.org
-
Here the SIMPLE project demo to illustrate the strange bug I noticed just some minutes ago. http://digilander.iol.it/ilbanca/fuffa/Bug.zip Compile in Debug mode and press the button. Compile in Release and press the button. It beheaves in different ways (bad in Release) The function that made me crazy: [code] void Buggy( char* result ) { char s1[6]; char s2[5]; strcpy(s1,"abcde"); strcpy(s2,"BUG!"); // DOESNT WORK strcpy(result,"anystring"); strcat(result,s1); // THIS WAY WORKS // strcpy(result,s1); } [/code] In Debug mode works In Release mode not Setting 'Optimizations' to "Default" instead of "Maximixe Speed" solve the problem, but why? I want to append 's1' to 'result', and it appends 's2' :(((((((((( Bye.
Which SP are you running for VC6? Either SP5 or SP6 is the latest. Tim Smith I know what you're thinking punk, you're thinking did he spell check this document? Well, to tell you the truth I kinda forgot myself in all this excitement. But being this here's CodeProject, the most powerful forums in the world and would blow your head clean off, you've got to ask yourself one question, Do I feel lucky? Well do ya punk?
-
Which SP are you running for VC6? Either SP5 or SP6 is the latest. Tim Smith I know what you're thinking punk, you're thinking did he spell check this document? Well, to tell you the truth I kinda forgot myself in all this excitement. But being this here's CodeProject, the most powerful forums in the world and would blow your head clean off, you've got to ask yourself one question, Do I feel lucky? Well do ya punk?
-
Your code works fine for me as well in both Debug and Release builds on WinXP Pro. That is I "anystringabcde" Have you tried rebooting Windows? If all else fails... Neville Franks, Author of ED for Windows. www.getsoft.com