The Software Purist |

CAT | Visual C++

Mar/13

20

Dangerous Memory Leak in TR1 shared_ptr class

I encountered a major/dangerous memory leak using std::tr1::shared_ptr. There is an edge case which will not delete the memory nor call the destructors. I’ll demonstrate:

Assume MessageFactory::GetMessage() returns a raw pointer of type IMessage for illustration purposes.


#include "IMessage.h"

...

for (size_t index = 0; index < 100; ++index) { std::tr1::shared_ptr pMessage(MessageFactory::GetMessage());
MessageFactory::ProcessMessage(pMessage.get());
}

This deletes the memory just fine. However, note that this block of code is actually not calling any members, so a forward declaration could also be used. So, instead:


class IMessage;

...

for (size_t index = 0; index < 100; ++index) { std::tr1::shared_ptr pMessage(MessageFactory::GetMessage());
MessageFactory::ProcessMessage(pMessage.get());
}

It actually does not have a definition for the destructor here, so it appears to simply skip over it altogether, and with it, it doesn’t call the destructors of the member variables, thus initiating a chain of some very dangerous leaks.

Conversely, in this same example, if we use boost::shared_ptr instead, it knows it won’t be able to call the destructor, so it fails to compile:


class IMessage;

...

for (size_t index = 0; index < 100; ++index) { boost::shared_ptr pMessage(MessageFactory::GetMessage());
MessageFactory::ProcessMessage(pMessage.get());
}


Error 1 error C2027: use of undefined type 'IMessage' c:\dev\common\3rdparty\boost_1_49_0\boost\checked_delete.hpp 32 SharedPtrLeakTest
Error 2 error C2118: negative subscript c:\dev\common\3rdparty\boost_1_49_0\boost\checked_delete.hpp 32 SharedPtrLeakTest

The ideal solution in this case would’ve been boost::scoped_ptr, which produces the same error messages.

Conclusion: We should not use std::tr1::shared_ptr, as the Microsoft implementation appears to be broken. It should not allow compilation for this invalid case. Please use the boost smart pointers (shared_ptr, scoped_ptr, shared_array, scoped_array, weak_ptr) until further notice instead of std::tr1::shared_ptr.

No tags

Dec/09

5

Visual Studio Helpers: VS Plugins Edition

I wanted to write in here with some of my favorite Visual Studio Tools, that I find useful in my every day work.

VSFileFinder

VSFileFinder has a very simple concept: You have a lot of files, but it takes a while to navigate for which project they’re in.  Not so with VSFileFinder.  This helpful tool allows you to type a few letters of a file or even use wildcards in your search and helps you find the files you’re looking for.  The premise is simple, but this tool definitely meshes well with my workflow.

CppDoc Comment Maker

CppDoc comment maker gives you an easy to fill out template for generating a new comment that conforms to the JavaDoc standard.  Again, there isn’t much to say here, other than it streamlines the workflow making this process easier.  I will admit, I haven’t used this one as much lately, since I discovered Visual Assist X (below).

Visual Assist X

Visual Assist X is really a lifesaver, especially if you do a lot of C++ work.  It has an incredible amount of features, including syntax highlighting, improved intellisense and refactoring.  The syntax highlighting in Visual Assist X is more intuitive and easier to follow than the default in Visual Studio.  The intellisense performs better, and actually seems to do more than Visual Studio Intellisense typically does.  I find it’s less prone to getting “confused”.  Finally, the refactoring is a godsend.  Refactoring tools in C++ are almost non-existent.  So, while Visual Assist X doesn’t provide as many refactoring capabilities as you get in Java IDEs, it does a good job.  Furthermore, you can edit and configure the snippets, to further customize your refactoring.  For this reason, I actually don’t use CppDoc Comment Maker so much, because I can use Visual Assist X to generate a CppDoc-style comment once I edit the template.  At a later date, I plan to share some of my useful Visual Assist X snippets that I use.

· · · ·

Theme Design by devolux.nh2.me