Fix segmentation faults

Fix places where invalid pointers are being freed, while handling error cases.

Open-webOS-DCO-1.0-Signed-off-by: Sapna Todwal <sapna.todwal@lge.com>

Change-Id: Ia9bcbf4fd84fd2e5809abdb201c82b41eb190302
Reviewed-on: http://g2g.palm.com/1623
Reviewed-by: Susan Montooth <susan.montooth@lge.com>
Reviewed-by: Ed Chejlava <ed.chejlava@palm.com>
Reviewed-by: Gayathri Srinivasan <gayathri.srinivasan@palm.com>
Tested-by: Sapna Todwal <sapna.todwal@lge.com>
Reviewed-by: Sapna Todwal <sapna.todwal@lge.com>