I wrote the code as below.
rows, err := s.db.QueryContext(context.TODO(), "select * from mytable")
if err != nil {
log.WithFields(log.Fields{
"query": queryString,
}).Errorln("failed to query, ", err)
return err
}
resultString, err := getResultString(rows)
Also I added the getResultString
as below.
func getResultString(rows *sql.Rows) (*string, error) {
defer rows.Close()
var result string
if rows.Next() {
if err := rows.Scan(&result); err != nil {
return nil, fmt.Errorf("scan err, %v", err)
} else {
return &result, nil
}
}
return nil, fmt.Errorf("query err, %v", rows.Err())
}
This code is wrote based on the example of how to use the query in the gosnowflake.
While running the app, sometimes the memory issues occurred as below.
2024-05-14T13:42:08.689+09:00 panic: runtime error: invalid memory address or nil pointer dereference
2024-05-14T13:42:08.689+09:00 panic: runtime error: invalid memory address or nil pointer dereference
2024-05-14T13:42:08.689+09:00 [signal SIGSEGV: segmentation violation code=0x1 addr=0x38 pc=0x7d8848]
2024-05-14T13:42:08.689+09:00 goroutine 143 [running]:
2024-05-14T13:42:08.689+09:00 github.com/snowflakedb/gosnowflake.(*snowflakeRows).Close(0x10?)
2024-05-14T13:42:08.689+09:00 /home/ec2-user/go/pkg/mod/github.com/snowflakedb/[email protected]/rows.go:67 +0x18
2024-05-14T13:42:08.689+09:00 database/sql.(*Rows).close.func1()
2024-05-14T13:42:08.689+09:00 /usr/lib/golang/src/database/sql/sql.go:3287 +0x34
2024-05-14T13:42:08.689+09:00 database/sql.withLock({0xb788d8, 0x40033dbc20}, 0x40008ecf48)
2024-05-14T13:42:08.689+09:00 /usr/lib/golang/src/database/sql/sql.go:3405 +0x7c
2024-05-14T13:42:08.689+09:00 database/sql.(*Rows).close(0x4000097980, {0x0, 0x0})
2024-05-14T13:42:08.689+09:00 /usr/lib/golang/src/database/sql/sql.go:3286 +0x130
After struggling to fix this issue, but I suspect the one point.
As you can see above, I created the rows in the function and passed them to another function (getResultString
) as an argument. While this procedure, I think that some memory corruption might occur because the code examples that I have seen is all the logics is included in the one function, which is contrast to my code.
So I suspect that the passing the row
to other function can lead to potential problem.
Is my suspect reasonable? Otherwise, do you have any suggestion for resolving the issue?
Thanks,