Skip to content

Commit

Permalink
fix: sqlite memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkAfCod authored and GrapeBaBa committed May 31, 2024
1 parent 02b87d9 commit 3e8973d
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions portalnetwork/history/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,17 +204,16 @@ func (p *ContentStorage) Close() error {
}

func (p *ContentStorage) createTable() error {
stat, err := p.sqliteDB.Prepare(createSql)
stmt, err := p.sqliteDB.Prepare(createSql)
if err != nil {
return err
}
defer func(stat *sql.Stmt) {
err = stat.Close()
if err != nil {
if err = stat.Close(); err != nil {
p.log.Error("failed to close statement", "err", err)
}
}(stat)
_, err = stat.Exec()
}(stmt)
_, err = stmt.Exec()
return err
}

Expand Down Expand Up @@ -243,13 +242,7 @@ func (p *ContentStorage) initStmts() error {
// Size get database size, content size and similar
func (p *ContentStorage) Size() (uint64, error) {
sql := "SELECT page_count * page_size as size FROM pragma_page_count(), pragma_page_size();"
stmt, err := p.sqliteDB.Prepare(sql)
if err != nil {
return 0, err
}
var res uint64
err = stmt.QueryRow().Scan(&res)
return res, err
return p.queryRowUint64(sql)
}

func (p *ContentStorage) UnusedSize() (uint64, error) {
Expand Down Expand Up @@ -281,12 +274,17 @@ func (p *ContentStorage) ContentSize() (uint64, error) {
return p.queryRowUint64(sql)
}

func (p *ContentStorage) queryRowUint64(sql string) (uint64, error) {
func (p *ContentStorage) queryRowUint64(sqlStr string) (uint64, error) {
// sql := "SELECT SUM(length(value)) FROM kvstore"
stmt, err := p.sqliteDB.Prepare(sql)
stmt, err := p.sqliteDB.Prepare(sqlStr)
if err != nil {
return 0, err
}
defer func(stat *sql.Stmt) {
if err = stat.Close(); err != nil {
p.log.Error("failed to close statement", "err", err)
}
}(stmt)
var res uint64
err = stmt.QueryRow().Scan(&res)
return res, err
Expand All @@ -298,6 +296,11 @@ func (p *ContentStorage) GetLargestDistance() (*uint256.Int, error) {
if err != nil {
return nil, err
}
defer func(stat *sql.Stmt) {
if err = stat.Close(); err != nil {
p.log.Error("failed to close statement", "err", err)
}
}(stmt)
var distance []byte

err = stmt.QueryRow(p.nodeId[:]).Scan(&distance)
Expand Down

0 comments on commit 3e8973d

Please sign in to comment.