@@ -741,6 +741,8 @@ void vector_context_free (void *p) {
741741 for (int i = 0 ; i < ctx -> table_count ; ++ i ) {
742742 if (ctx -> tables [i ].t_name ) sqlite3_free (ctx -> tables [i ].t_name );
743743 if (ctx -> tables [i ].c_name ) sqlite3_free (ctx -> tables [i ].c_name );
744+ if (ctx -> tables [i ].pk_name ) sqlite3_free (ctx -> tables [i ].pk_name );
745+ if (ctx -> tables [i ].preloaded ) sqlite3_free (ctx -> tables [i ].preloaded );
744746 }
745747 sqlite3_free (p );
746748 }
@@ -1433,12 +1435,17 @@ static int vCursorFilterCommon (sqlite3_vtab_cursor *cur, int idxNum, const char
14331435 // nothing needs to be returned
14341436 if (k == 0 ) return SQLITE_DONE ;
14351437
1436- c -> rowids = (sqlite3_int64 * )sqlite3_malloc (k * sizeof (sqlite3_int64 ));
1437- if (c -> rowids == NULL ) return SQLITE_NOMEM ;
1438- memset (c -> rowids , 0 , k * sizeof (sqlite3_int64 ));
1438+ if (c -> row_count != k ) {
1439+ if (c -> rowids ) sqlite3_free (c -> rowids );
1440+ c -> rowids = (sqlite3_int64 * )sqlite3_malloc (k * sizeof (sqlite3_int64 ));
1441+ if (c -> rowids == NULL ) return SQLITE_NOMEM ;
1442+
1443+ if (c -> distance ) sqlite3_free (c -> distance );
1444+ c -> distance = (double * )sqlite3_malloc (k * sizeof (double ));
1445+ if (c -> distance == NULL ) return SQLITE_NOMEM ;
1446+ }
14391447
1440- c -> distance = (double * )sqlite3_malloc (k * sizeof (double ));
1441- if (c -> distance == NULL ) return SQLITE_NOMEM ;
1448+ memset (c -> rowids , 0 , k * sizeof (sqlite3_int64 ));
14421449 for (int i = 0 ; i < k ; ++ i ) c -> distance [i ] = INFINITY ;
14431450
14441451 c -> size = 0 ;
@@ -1695,7 +1702,11 @@ static int vQuantRun (sqlite3 *db, vFullScanCursor *c, const void *v1, int v1siz
16951702 if (!v ) return SQLITE_NOMEM ;
16961703
16971704 quantize_float32_to_u8 ((float * )v1 , v , c -> table -> offset , c -> table -> scale , dimension );
1698- if (c -> table -> preloaded ) return vQuantRunMemory (c , v , dimension );
1705+ if (c -> table -> preloaded ) {
1706+ int rc = vQuantRunMemory (c , v , dimension );
1707+ if (v ) sqlite3_free (v );
1708+ return rc ;
1709+ }
16991710
17001711 char sql [STATIC_SQL_SIZE ];
17011712 generate_select_quant_table (c -> table -> t_name , c -> table -> c_name , sql );
@@ -1743,6 +1754,7 @@ static int vQuantRun (sqlite3 *db, vFullScanCursor *c, const void *v1, int v1siz
17431754kann_run_cleanup :
17441755 if (rc != SQLITE_OK ) printf ("Error in vector_rebuild_quantization: %s\n" , sqlite3_errmsg (db ));
17451756 if (vm ) sqlite3_finalize (vm );
1757+ if (v ) sqlite3_free (v );
17461758 return rc ;
17471759}
17481760
0 commit comments