SQLite Forum

Is the lifetime of argv in sqlite3_module::xConnect greater than sqlite3_module::xFilter?
Login

Is the lifetime of argv in sqlite3_module::xConnect greater than sqlite3_module::xFilter?

(1) By Deon Brewis (deonb) on 2021-10-01 22:45:57 [link] [source]

If I have a SQLITE virtual table, can I store a direct pointer value from argv (const char*) inside sqlite3_module::xConnect to use later in a call to sqlite3_module::xFilter? Specifically the address of the module name inside argv[0].

Basically the type name of my underlying pointer type is the same as the module name of the Virtual Table, so it would be used for sqlite3_value_pointer during xFilter.

I just need to know whether the lifetime of the module name is guaranteed for the life of the Virtual Table, or do I need to deep copy it.

(It seems to work).

(2) By Larry Brasfield (larrybr) on 2021-10-01 23:31:20 in reply to 1 [link] [source]

If I have a SQLITE virtual table, can I store a direct pointer value from argv (const char*) inside sqlite3_module::xConnect to use later in a call to sqlite3_module::xFilter? Specifically the address of the module name inside argv[0].

My study of the relevant docs shows them to be silent on the issue of argument lifetimes. So your question has several answers of varying utility.

I just need to know whether the lifetime of the module name is guaranteed for the life of the Virtual Table, or do I need to deep copy it.

I find no such guarantee, and I was looking for it carefully. I would think that means the usual guarantee applies: The passed argument values (whether indirect or not) can be relied upon during execution of the call. No longer lifetime is guaranteed.

(It seems to work).

Lots of things doomed to fail one day do not do so conveniently soon. That can be seen as bad luck or as a design failure.

I looked at the implementation calling into that xConnect method, and it looked like no needless copies were made. Whether that will change I dare not predict. So it appears that the same pointer you provided earlier is reaching your xConnect method. Is that so? And if so, I suggest, (plagiarizing): You've got to ask yourself one question: 'Do I feel lucky?'.

(3) By Gunter Hick (gunter_hick) on 2021-10-04 09:18:46 in reply to 1 [source]

There are at least two possible sources for the module name passed to the xConnect method.

- it could be the argument passed to the sqlite3_create_module[_v2] function, in which case it's lifetime is determined by you, the caller

- it could be a copy of the above, stored in SQLite internal structures associated with the module, which would then probably be valid until the module is dropped

- it could be the argument given in the CREATE VIRTUAL TABLE statement, in which case it could reside in the input buffer, or inside the SQL program created to process the statement, or somewhere else convenient for SQLite; all of which  probably expire after your xConnect function returns