Monday, September 18, 2017

Visual Studio Debugging Error and Solution

About a week ago I was working on a Programming Challenge. The debugger was running, and the only breakpoint was set on a string member variable. Hovering over the [allocator] section to see what was going, things changed dramatically. An error message?

_Ptr = 0x325785 <Error reading characters of string.>  

What is going on here? Have I done something wrong? Is it some problem with Visual Studio? Could it be a component of my security suite that is causing this error? Whatever it was, why was the program running and compiling and building just fine? Why was the output as expected? Why didn't the program freeze or crash if there is indeed an error?

My best bet seemed to be to look this error up on the Internet. To my surprise the results matching my search were sparse. Microsoft does seem to be aware that the error exists, but no bugfix or solution seems to exist for it. One of the few solutions I found didn't work either: "Disable all Optimizations in the project properties. (Alt + F7), C/C++, Optimizations and Linker, Optimizations." 


I was left to my own devices to solve the problem ... Starting up VS, setting up a new project, toying around with the project settings, trying a different framework, nothing helped. Next I was loading some of my old projects to see whether this problem occurs or doesn't and it did. Days went by without getting any closer to the root of the problem or finding a solution for it, I decided to wipe my system. With a clean installation of both the OS and VS my hope was that things would take a turn for the better. They did not ...

Back on square one I had two options:

One: Ignore it. Impossible ... 
Two: Trying my luck on finding a solution Online once more, ...

Before going Online, I set up another project in VS:

#include <string>
#include <iostream>

int main()

{
    std::string myLittlePonyOne = "Rarity";
    std::string myLittlePonyTwo = "Twilight Sparkle";

    std::cout << "My favorite My Little Pony Characters are:\n"
                  << myLittlePonyOne << " \n"
                  << myLittlePonyTwo << " \n";

    std::cin.ignore();
    return 0;
}

I didn't debug it right away, else I would have discovered that this little program contains the solution to the problem. Had I done it without looking for a solution, I wouldn't have found the answer to the question why this error was occurring and what was causing it. Here is the 'solution' or rather answer to the problem.

The STL string classes contain an optimization called Small String Optimization or SSO for short. To learn more about it, please visit Giovanni Dicanio's C++ Corner on the Internet. Having read this blog-post, I immediately went back to the IDE, started the debugger, and found that this was the source of the 'error'. Consider the following screenshots:


As "Rarity" is very short, it is a small variable, so there is no memory allocated on the heap, resulting in this 'error' message. 


Not so in case of "Twilight Sparkle", which is long enough, resulting in correct behavior. 

After carrying out some more tests just to be sure I found that this was indeed the source of the 'error' that is anything but. "It's not a bug, it's a feature", or in this case "It's not a bug, it's an optimization", to coin a new phrase. I'm just glad that in the end it turned out that all of my programs old and new containing string class in the end turn out not to be error-ridden.  

I sincerely hope that none of my readers, much less my fellow learners, will have to go through so much trouble ... With this, I wish my readers and fellow learners a successful new week, and thank you very much for your visit and continued interest in this modest blog. As for me, it is time to write more code, and to overcome more errors and bugs.

No comments:

Post a Comment