Fix to bug #612, http://bugs.mariadb.com/show_bug.cgi?id=612
Also added error logging in cases where memory allocation failed etc.