Archive

Archive for the ‘Strange bugs’ Category

std::string caveat

Never access an std::string‘s buffer with an intent to increase/decrease it’s length nor pass such a buffer to functions which takes a char*. I did this mistake sometime back and got trapped in a strange bug with operator +=. This is how my code looked.

std::string str( ' ', MAX_PATH );
GetFolderName( pFullPath, &str[0] ); // Oop

Problem with above code is that you won’t get an immediate crash since it’s a properly allocated buffer with MAX_PATH chars. But if you do further operations on such string expect plenty of inconsistencies. This is how my code looked after above piece of code…

str += "\\"; // Append backslash

I was expecting a valid path with backslash appended towards it’s end, but this never happened and debugging with debugger too didn’t help.

So now let me tell you what the exact problem is! When you do &str[0] you pass the address to the first char in std::string‘s buffer. So when function GetFolderName fills in this buffer with folder path the length of std::string is not updated since it’s a C style function and it’s neither expected to do so. So the function terminates given buffer with a ” with std::string‘s length way high (MAX_PATH). So now when I do a += std::string internally fails some condition leading to unexpected results, note that this piece of code never caused a crash but I was quite lucky and watchful enough to fix this stupid bug. Sigh!

So watchout, there is no CString::GetBuffer or CString::GetBufferSetLength type of function for std::string, well at least for now.

Project Conversion Bug in VS2008

May 11, 2009 1 comment

Are you having trouble with VS2008 after conversion from VS2005 to VS2008? Most common complaints are that the executable is way to slow when compared to it’s counterpart generated with VS2005. The reason for this is given in this MSDN forum thread have a look…

http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/fb32033b-7bad-439b-a94c-943a17f0cbb2

The essence of this thread is given below (quote from the thread, thanks to Jon Baggott)…

In Visual Studio 2008 SP1 (SP1 not RTM) there is a serious bug with /O2 optimization. One way this bug can be triggered is by upgrading a project from a previous version. Even though the project setting shows the release build is set to /O2, the build can be not optimized at all. To work around it you have to change the setting to no optimization, apply, and then change it back to /O2. The quick way to see if this is needed is to check whether the setting for optimization is in bold or regular – if’s it’s bold you’re OK; if it’s regular text you’re not. This bug has been reported to Microsoft by many people over the past few months via the Connect feedback site but every case has been closed by them without doing anything and for completely invalid reasons.

Script error in VC++ wizards after installing IE8

April 1, 2009 3 comments

You might have script errors in VC++ wizards after installing IE8, for e.g. when you double click inside a dialog normally we should taken to corresponding file but instead we get a script error…

Script error

Script error

Pretty strange isn’t it. Well as per VC++ team blogs this bug due to IE8 installation. Reason for this behavior is as explained below (quoted from VC++ team blogs…)

The VC++ Wizards Engine implements the IInternetSecurityManager interface.  In this implementation it allows or disallows specific actions under certain policies that Internet Explorer queries it about.  In IE8 a custom Security Manager now also gets queried about the URLACTION_ACTIVEX_OVERRIDE_REPURPOSEDETECTION policy which IE previously did not delegate to custom security managers when the engine wasn’t running in the iexplore.exe process.  The IE engine then fails this action because we don’t have a policy entry for it in the custom zone for VC++ Wizards. We are still investigating whether this change in IE8 is by design and will possibly be addressing it by a fix in either the Wizard or IE components depending on the outcome.

And according to this blog, following fix should work….

Please follow the following steps:
  • Open regedit (on a 64-bit OS, open the 32-bit regedit)
  • Under “HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet
    Settings\Zones”, create a new key called 1000 (if it isn’t already there)
  • Under 1000, create a DWORD entry with:
    • o Name = 1207
    • o Type = REG_DWORD
    • o Data = 0x000000

Just now I had this problem hence I had to use this fix and it works. 🙂

Choosing between a function and a macro definition

June 26, 2008 2 comments

This is a common and a funny problem. So what is this all about? Let me put it like this…

We have a standard “c” function called _toupper and an equivalent macro is also available called _toupper. So my question is how to call function version of _toupper explicitly, if we write _toupper, macro version  get expanded inline, since preprocessor runs before compiler.

Here is the solution…

#include <ctype .h>

const char upper = _toupper( ch ); // Will invoke macro version
const char upper = (_toupper)(ch); // Will invoke function

Another routine is _fileno, implemented both as a function and as a macro. Apply same trick.

const int ErrFileNo = (_fileno)( stderr );
const int OutFileNo = _fileno( stdout );

Another option is to use #undef to undefine the macro definition.

#include </ctype><ctype .h>
#undef _toupper

A friend of mine was saying that he did encounter such bugs due to such “automagic” macro expansion.

Variable value is always’s zero, no matter what you do!

May 8, 2008 3 comments

Recently a friend of mine (sitting just behind me) had a strange problem. I wrote an inline function which was like…

void SetBlah( const bool IsBlah ) { m_IsBlah = IsBlah; }

Now he was using this function since we work for the same project! So he had a value 1 for IsBlah when calling this function but no matter what he did m_IsBlah always had the value zero!

He spent half a day trying to solve this issue, I too joined him in this venture. We thought it has got something to do with inlining the function call so we made it non inlined but this didn’t solve the issue. No matter what we did still the value was zero, we tried rebuilding the project but no effect whatsoever.

I tried running the code on my machine and it was fine, working as it should be!

So in the end I found out the problem? But before I post the answer here, any idea what could be the reason?

Answer

He had a statement in his watch window like…

theStaticLongNameObject.m_IsBlah = 0;

This expression prevented the value from changing. Also we can’t see the full expression in watch window because of the variable’s long name! 🙂

%d bloggers like this: