bind blob lifetime

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

bind blob lifetime

Dave Milter
I have cycle like this:

```c
const char sql[] = "INSERT INTO test (pk, geom) VALUES (?, ?)";
sqlite3_stmt *stmt;
sqlite3_prepare_v2 (handle, sql, strlen (sql), &stmt, NULL);

for (...) {
        sqlite3_reset (stmt);
        sqlite3_clear_bindings (stmt);
        int blob_size = ..;
        unsigned char *blob = malloc(blob_size);
        sqlite3_bind_int64 (stmt, 1, pk);
        sqlite3_bind_blob (stmt, 2, blob, blob_size, free);
        sqlite3_step (stmt);
}
//sqlite3_finalize
```

I wonder is it necessary to allocate memory on every cycle?
I know that I can pass SQLITE_TRANSIENT, but in this case code would
be exactly the same, just allocation on every cycle happens inside
sqlite.

According to documentation  it is not clear when sqlite call
destructor of blob (in our case  "free"), is it happens after:
sqlite3_reset
sqlite3_clear_bindings
sqlite3_bind_blob
step

sqlite3_reset
sqlite3_clear_bindings
sqlite3_bind_blob
<< here previous memory was freed???
step

?
_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Reply | Threaded
Open this post in threaded view
|

Re: [EXTERNAL] bind blob lifetime

Hick Gunter
A bound blob or string is destroyed "after SQLite has finished with it". This should be the case when sqlite3_clear_bindings() is called. Are you sure it is not deleted then? Code reading suggests it should be.

Other times are when the parameter is re-bound, or the statement finalized.

-----Urspr√ľngliche Nachricht-----
Von: sqlite-users [mailto:[hidden email]] Im Auftrag von Dave Milter
Gesendet: Dienstag, 09. Jänner 2018 16:36
An: [hidden email]
Betreff: [EXTERNAL] [sqlite] bind blob lifetime

I have cycle like this:

```c
const char sql[] = "INSERT INTO test (pk, geom) VALUES (?, ?)"; sqlite3_stmt *stmt;
sqlite3_prepare_v2 (handle, sql, strlen (sql), &stmt, NULL);

for (...) {
        sqlite3_reset (stmt);
        sqlite3_clear_bindings (stmt);
        int blob_size = ..;
        unsigned char *blob = malloc(blob_size);
        sqlite3_bind_int64 (stmt, 1, pk);
        sqlite3_bind_blob (stmt, 2, blob, blob_size, free);
        sqlite3_step (stmt);
}
//sqlite3_finalize
```

I wonder is it necessary to allocate memory on every cycle?
I know that I can pass SQLITE_TRANSIENT, but in this case code would be exactly the same, just allocation on every cycle happens inside sqlite.

According to documentation  it is not clear when sqlite call destructor of blob (in our case  "free"), is it happens after:
sqlite3_reset
sqlite3_clear_bindings
sqlite3_bind_blob
step

sqlite3_reset
sqlite3_clear_bindings
sqlite3_bind_blob
<< here previous memory was freed???
step

?
_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users


___________________________________________
 Gunter Hick | Software Engineer | Scientific Games International GmbH | Klitschgasse 2-4, A-1130 Vienna | FN 157284 a, HG Wien, DVR: 0430013 | (O) +43 1 80100 - 0

May be privileged. May be confidential. Please delete if not the addressee.
_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Reply | Threaded
Open this post in threaded view
|

Re: [EXTERNAL] bind blob lifetime

Dave Milter
On Tue, Jan 9, 2018 at 7:28 PM, Hick Gunter <[hidden email]> wrote:
> A bound blob or string is destroyed "after SQLite has finished with it". This should be the case when sqlite3_clear_bindings() is called. Are you sure it is not deleted then? Code reading suggests it should be.
>
> Other times are when the parameter is re-bound, or the statement finalized.
>

Sorry for misunderstanding. I did not do any experiments, I only read
documentation couple of times about "bind_blob",
and it was not clear when destructor will be called by sqlite.
Thank you!
_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users