Also when you use the SelectObject function it returns a pointer to the old object. Reselect this object before the one you created goes out of scope. For example:
CPen pen1, *penOld;
penOld = pen1.CreatePen(PS_SOLID,3,RGB(0,0,0));
dc.SelectObject(&pen1);
// ....processes using the pen1 object...