diff --git a/sql/ha_ndbcluster.cc b/sql/ha_ndbcluster.cc
index b6f483ebc9111fd29c10951a43a1f0a1eef84c8f..a60c20c45ad7180490c1afd9a9c024adce5a6c06 100644
--- a/sql/ha_ndbcluster.cc
+++ b/sql/ha_ndbcluster.cc
@@ -8674,7 +8674,7 @@ pthread_handler_t ndb_util_thread_func(void *arg __attribute__((unused)))
     /* Lock mutex and fill list with pointers to all open tables */
     NDB_SHARE *share;
     pthread_mutex_lock(&ndbcluster_mutex);
-    uint i, record_count= ndbcluster_open_tables.records;
+    uint i, open_count, record_count= ndbcluster_open_tables.records;
     if (share_list_size < record_count)
     {
       NDB_SHARE ** new_share_list= new NDB_SHARE * [record_count];
@@ -8689,7 +8689,7 @@ pthread_handler_t ndb_util_thread_func(void *arg __attribute__((unused)))
       share_list_size= record_count;
       share_list= new_share_list;
     }
-    for (i= 0; i < record_count; i++)
+    for (i= 0, open_count= 0; i < record_count; i++)
     {
       share= (NDB_SHARE *)hash_element(&ndbcluster_open_tables, i);
 #ifdef HAVE_NDB_BINLOG
@@ -8707,12 +8707,12 @@ pthread_handler_t ndb_util_thread_func(void *arg __attribute__((unused)))
                   i, share->table_name, share->use_count));
 
       /* Store pointer to table */
-      share_list[i]= share;
+      share_list[open_count++]= share;
     }
     pthread_mutex_unlock(&ndbcluster_mutex);
 
     /* Iterate through the open files list */
-    for (i= 0; i < record_count; i++)
+    for (i= 0; i < open_count; i++)
     {
       share= share_list[i];
 #ifdef HAVE_NDB_BINLOG