Friday, May 4, 2012

The Rights of Spring

I'm chilling in my room before I head to the Majestic. I've seen some of my friends and ad breakfast with a couple of the lads from DeeExpus. The festival hasn't even started yet and I'm having a great time already.  If you are a fan of good music you should make the pilgramige to Gettysburg. You won't regret it.

Wednesday, May 2, 2012

Pre-Rites of Spring

I’m watching the clock as it winds down for my last working day before I leave for this years Rites of Spring festival. This will be the second time I’ve made the pilgrimage to Gettysburg for this festival. Last year I knew one person before going and came home with many new friends. I can only imagine how many friends I’ll make this year.

Last year I was only familiar with Tinyfish before the festival. I really enjoyed listening and discovering the other bands there. Many of whom have earned places on my playlists. This year I know DeeExpus and the Aaron English Band. I look forward to hearing both of them perform again and learning about the other performers this year.

This year I’m going to try and make some notes to blog more about my experience at RoSfest. No promises though. The late night parties have a way of eating up what little free time I have. Plus there’s meeting all my friends who I haven’t seen in far too long.

Is it time to get on the plane yet?

Tuesday, February 28, 2012

New Rules for a New Era

As I write this it’s 2012.  There’s a new C++ standard, C++11, and yet I still come across a lot of bad C++ code in the wild.  So I decided to use some of my time to write some posts that I hope new programmers will find and use to improve their skills.  Today I’m going to talk about a “rule” I found on a blog post from 2007 that was one of the top results when I searched the web for information about exceptions.

The rule is:

Avoid exceptions.  Exceptions will only leave you open to the memory and resource leaks. Don't use them. The exception to this rule is if you are programming in a style that doesn't use pointers, and everything is encapsulated into smart pointers.

The problem I see is the exception to the rule that should be the standard programming style followed.  There is rarely a need to program in a style that uses bare pointers.  Even when you need to use a bare pointer the standard library has facilities to make them exception safe.  The example given in the posting was:

void foo()
{
    Object *obj = new Object();
    bar();
    delet obj;
}

As you can see if bar() throws an exception the instance of Object is not released and we have a resource leak.  This is used as the argument of why we shouldn’t use exceptions.  I counter that this is an example of poor coding.  I would recommend two ways to rewrite the foo() function that remove the resource leak.

The first method would be to declare the instance of Object on the stack.  If an exception is thrown the resources would be released when the stack is unwound as part of the exception processing.  If Object contains large data members they should be allocated on the heap as part of construction and released during destruction.  This would change foo() to:

void foo()
{
    Object obj();
    bar();
}

The second method would be to use std::unique_ptr<> which would wrap the instance of object allocated on the heap with new.  The instance would also be released when the stack is unwound during exception handling as the wrapper includes a destructor to free the resource.  This version of foo() would be:

void foo()
{
    std::unique_ptr<Object> obj(new Object());
    bar();
}

I mentioned before it was 2012.  Microsoft Visual C++ 2010 has support for some C++11 features and std::unique_ptr<> is one of them.  So stop using bare pointers unless you have a real need for them, and I’ll bet that you do not have a real need for them.