Fix some memory leaks that occur when malloc() fails. (CVS 3286)

FossilOrigin-Name: b56cc035f2be5c1a3f63efbb4c181e405a140fbb
diff --git a/src/main.c b/src/main.c
index b72d219..ae5f99c 100644
--- a/src/main.c
+++ b/src/main.c
@@ -14,7 +14,7 @@
 ** other files are for internal use by SQLite and should not be
 ** accessed by users of the library.
 **
-** $Id: main.c,v 1.346 2006/06/22 09:53:49 danielk1977 Exp $
+** $Id: main.c,v 1.347 2006/06/23 11:34:55 danielk1977 Exp $
 */
 #include "sqliteInt.h"
 #include "os.h"
@@ -133,6 +133,8 @@
     return SQLITE_ERROR;
   }
 
+  sqlite3VtabRollback(db);
+
   for(j=0; j<db->nDb; j++){
     struct Db *pDb = &db->aDb[j];
     if( pDb->pBt ){
diff --git a/src/test8.c b/src/test8.c
index 89ba964..842000d 100644
--- a/src/test8.c
+++ b/src/test8.c
@@ -13,7 +13,7 @@
 ** is not included in the SQLite library.  It is used for automated
 ** testing of the SQLite library.
 **
-** $Id: test8.c,v 1.32 2006/06/23 08:05:26 danielk1977 Exp $
+** $Id: test8.c,v 1.33 2006/06/23 11:34:55 danielk1977 Exp $
 */
 #include "sqliteInt.h"
 #include "tcl.h"
@@ -259,6 +259,7 @@
   pVtab->db = db;
   pVtab->zTableName = sqlite3MPrintf("%s", argv[3]);
   if( !pVtab->zTableName ){
+    echoDestructor((sqlite3_vtab *)pVtab);
     return SQLITE_NOMEM;
   }
 
diff --git a/src/vdbe.c b/src/vdbe.c
index 9fb569d..8d76559 100644
--- a/src/vdbe.c
+++ b/src/vdbe.c
@@ -43,7 +43,7 @@
 ** in this file for details.  If in doubt, do not deviate from existing
 ** commenting and indentation practices when changing or adding code.
 **
-** $Id: vdbe.c,v 1.566 2006/06/23 08:05:26 danielk1977 Exp $
+** $Id: vdbe.c,v 1.567 2006/06/23 11:34:55 danielk1977 Exp $
 */
 #include "sqliteInt.h"
 #include "os.h"
@@ -4600,6 +4600,8 @@
     if( pCur ){
       pCur->pVtabCursor = pVtabCursor;
       pCur->pModule = pVtabCursor->pVtab->pModule;
+    }else{
+      pModule->xClose(pVtabCursor);
     }
   }
   break;
diff --git a/src/vdbeaux.c b/src/vdbeaux.c
index 6d524f8..d9b54e0 100644
--- a/src/vdbeaux.c
+++ b/src/vdbeaux.c
@@ -875,8 +875,8 @@
   for(i=0; i<p->nCursor; i++){
     if( !p->inVtabMethod || (p->apCsr[i] && !p->apCsr[i]->pVtabCursor) ){
       sqlite3VdbeFreeCursor(p, p->apCsr[i]);
+      p->apCsr[i] = 0;
     }
-    p->apCsr[i] = 0;
   }
 }
 
@@ -1250,6 +1250,9 @@
   if( p->magic!=VDBE_MAGIC_RUN ){
     /* Already halted.  Nothing to do. */
     assert( p->magic==VDBE_MAGIC_HALT );
+#ifndef SQLITE_OMIT_VIRTUALTABLE
+    closeAllCursors(p);
+#endif
     return SQLITE_OK;
   }
   closeAllCursors(p);
diff --git a/src/vtab.c b/src/vtab.c
index 74824fc..4af8be8 100644
--- a/src/vtab.c
+++ b/src/vtab.c
@@ -11,7 +11,7 @@
 *************************************************************************
 ** This file contains code used to help implement virtual tables.
 **
-** $Id: vtab.c,v 1.21 2006/06/23 08:05:30 danielk1977 Exp $
+** $Id: vtab.c,v 1.22 2006/06/23 11:34:55 danielk1977 Exp $
 */
 #ifndef SQLITE_OMIT_VIRTUALTABLE
 #include "sqliteInt.h"
@@ -72,15 +72,22 @@
 */
 static void addModuleArgument(Table *pTable, char *zArg){
   int i = pTable->nModuleArg++;
-  pTable->azModuleArg = sqliteRealloc(pTable->azModuleArg,
-                             sizeof(char*)*(pTable->nModuleArg+1));
-  if( pTable->azModuleArg==0 ){
-    pTable->nModuleArg = 0;
+  int nBytes = sizeof(char *)*(1+pTable->nModuleArg);
+  char **azModuleArg;
+  azModuleArg = sqliteRealloc(pTable->azModuleArg, nBytes);
+  if( azModuleArg==0 ){
+    int j;
+    for(j=0; j<i; j++){
+      sqliteFree(pTable->azModuleArg[j]);
+    }
     sqliteFree(zArg);
+    sqliteFree(pTable->azModuleArg);
+    pTable->nModuleArg = 0;
   }else{
-    pTable->azModuleArg[i] = zArg;
-    pTable->azModuleArg[i+1] = 0;
+    azModuleArg[i] = zArg;
+    azModuleArg[i+1] = 0;
   }
+  pTable->azModuleArg = azModuleArg;
 }
 
 /*