C++: Your best friend and worst enemy

I just spent 2 hours looking for a bug until I found it in this rather innocuous bit of code:

SyncItem* item = NULL;
if (ret)
{
    SyncItem* item = new SyncItem;
    item->setKey(key);
    item->setData(data, memUsed);
}
if (data != NULL)
    LocalFree(data);
return item;

2 comments:

Jared Parsons said...

I prefer to think of C++ as a friend. Let's face it we've all had a friend that's just like C++. Fun to hang out with, good at parties and really helps you out from time to time.

Like a true friend, if you get stuck in jail C++ will be there with you. Unfortunately C++ will also fess up the lawyers for a get out of jail free card while simultaneously selling you out.

Koush said...

Hahaha, that analogy made a surprising amount of sense.