Re: [sqlite] Help with Backup API please

2017-02-08 Thread Barry
Ok. My bad for singling out SQLite, I should have rather mentioned that the
problem exists with all memory allocation routines (malloc/free or
new/delete), at least on windows.

A workaround is to pass around allocator / deallocator function pointers
with any data structure which contains pointers which might be free'd or
realloc'd in a different module. My assumption was that SQLite does not do
this, since being Lite is a main priority and if one is using multiple
modules then option to dynamically link to SQLite avoids the problem.

My reason for assuming memory corruption (or invalid call to free(...)) was
the fact that Brett is encountering an exception rather than an invalid
return code.

On 8 February 2017 at 14:16, Richard Hipp  wrote:

> On 2/8/17, Barry Smith  wrote:
> >
> > I believe SQLite doesn't use the standard memory allocation routines, but
> > instead has its own routines.
>
> SQLite has its on memory allocation routines (if you use the right
> compile-time and start-time options) but it uses system
> malloc()/realloc()/free() by default.
>
> There are internal wrappers routines for the memory allocator that
> dispatch requests to the appropriate implementation.  Again, unless
> you take special actions to change it, memory allocation request are
> satisfied by malloc().
>
> --
> D. Richard Hipp
> d...@sqlite.org
> ___
> sqlite-users mailing list
> sqlite-users@mailinglists.sqlite.org
> http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
>
___
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users


Re: [sqlite] Help with Backup API please

2017-02-08 Thread Richard Hipp
On 2/8/17, Barry Smith  wrote:
>
> I believe SQLite doesn't use the standard memory allocation routines, but
> instead has its own routines.

SQLite has its on memory allocation routines (if you use the right
compile-time and start-time options) but it uses system
malloc()/realloc()/free() by default.

There are internal wrappers routines for the memory allocator that
dispatch requests to the appropriate implementation.  Again, unless
you take special actions to change it, memory allocation request are
satisfied by malloc().

-- 
D. Richard Hipp
d...@sqlite.org
___
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users


Re: [sqlite] Help with Backup API please

2017-02-08 Thread Barry Smith
Hi Brett,

I believe SQLite doesn't use the standard memory allocation routines, but 
instead has its own routines. These (might) use global variables. If each 
module of your application statically links to the SQLite source rather than 
having SQLite in a common DLL, then each module will have its own set of global 
variables. As such, passing raw SQLite pointers across module boundaries can 
result in undefined behaviour. I suspect other issues could also occur with 
other global variables in SQLite.

Note that passing the raw pointer is not the real culprit, but rather having 
different modules attempt to work on the pointer. For example:, the following 
should work fine:
sqlite3* pDb = DLL1GetDb();
DLL1DoSomething(pDb);

But either of the following could fail:
sqlite3* pDb = DLL1GetDb();
DLL2DoSomething(pDb);
// or, if the calling module is not DLL1
sqlite3_exec(pDb,...);

Personally, to enforce this I do not expose (either in arguments or return 
values) raw sqlite objects (dbs, statements, or anything else) in exported 
functions.

If you already have SQLite in a common DLL, or you perform all sqlite3_* calls 
from the same module, I apologise for wasting your time.

Also, SQLite is written in C so it uses error return codes rather than throwing 
exceptions. The exception you are catching would have to be thrown by another 
library that SQLite is calling. This would mean that the output of 
sqlite3_errmsg is likely unrelated to the exception since SQLite would never 
have intercepted it. Perhaps your debugging environment could tell you more 
details of the real proximate cause of the error (unless the you mean that the 
error message of the exception, by coincidence, was the same as a standard 
SQLite error message)

Cheers,

 - Barry 

> On 8 Feb 2017, at 9:30 AM, Brett Goodman  wrote:
> 
> Hello Sqlite users.  I have a problem I can't solve.  I have a C++ DLL 
> project in which I compile the Sqlite amalgamation code. The purpose is to 
> wrap the key Sqlite functions with exported functions I can call from another 
> C++ COM DLL.   I'm using VS2003 for this because its part of a legacy 
> product.  I've been using this project for years, updating the Sqlite source 
> code from time to time with no problems.  I can call functions like 
> sqlite3_open_v2, sqlite3_prepare, sqlite3_step, etc. with no problems.  Now 
> I'm trying to integrate the Backup API.  When I call sqlite3_backup_init it 
> throws this error: _/"library /__/
> /__/routine called out of sequence"/_.  I want to step into the Sqlite code 
> to find out why but for some reason my debugger won't allow me to step into 
> the C code from my C++ code.  So I'm hoping someone can help me.  Below is my 
> C++ implementation which I modeled from the sample on this page: 
> sqlite.org/backup.html.  My calling code is from a VB6 project which simply 
> creates/opens a memory DB and calls the below function (via my COM DLL).  Any 
> help is much appreciated.
> 
> Thanks,
> BrettG
> 
> 
> SQLITE_DLL_API int backup( /*sqlite3*/ void* pdb, const WCHAR *pFilename, 
> BOOL toFile )
> {
>  int rc;
>  sqlite3 *pFileDb;
>  sqlite3_backup *pBackup;
>  sqlite3 *pTo;
>  sqlite3 *pFrom;
> 
>  CTextConverter c(CP_UTF8);
>  //rc = sqlite3_open_v2( c.U2M(pFilename), , SQLITE_OPEN_READWRITE, 
> NULL);
>  rc = sqlite3_open( c.U2M(pFilename), );
> 
>  if( rc==SQLITE_OK )
>  {
>try
>{
>sqlite3 *pMemoryDb = (sqlite3*) pdb;
>pFrom = (toFile ? pMemoryDb : pFileDb);
>pTo   = (toFile ? pFileDb : pMemoryDb);
> 
>// this line throws exception:  "library routine called out of 
> sequence"
> *pBackup = sqlite3_backup_init(pTo, "main", pFrom, "temp");*
>if( pBackup )
>{
>sqlite3_backup_step(pBackup, -1);
>sqlite3_backup_finish(pBackup);
>}
>rc = sqlite3_errcode(pTo);
>sqlite3_close(pFileDb);
>}
>catch(...)
>{
>sqlite3_close(pFileDb);
>const char* err = sqlite3_errmsg( (sqlite3*) pdb );
>throw err;
>}
>  }
>  return rc;
> }
> 
> ___
> sqlite-users mailing list
> sqlite-users@mailinglists.sqlite.org
> http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
___
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users


Re: [sqlite] Help with Backup API please

2017-02-08 Thread Clemens Ladisch
Brett Goodman wrote:
> When I call sqlite3_backup_init it throws this error: _/"library /__/
> /__/routine called out of sequence"/_.

To you get an error code, or an exception?
In the first case, try calling sqlite3_errmsg().

The documentation says:
| A call to sqlite3_backup_init() will fail, returning NULL, if there is
| already a read or read-write transaction open on the destination
| database.


Regards,
Clemens
___
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users


[sqlite] Help with Backup API please

2017-02-08 Thread Brett Goodman
Hello Sqlite users.  I have a problem I can't solve.  I have a C++ DLL 
project in which I compile the Sqlite amalgamation code. The purpose is 
to wrap the key Sqlite functions with exported functions I can call from 
another C++ COM DLL.   I'm using VS2003 for this because its part of a 
legacy product.  I've been using this project for years, updating the 
Sqlite source code from time to time with no problems.  I can call 
functions like sqlite3_open_v2, sqlite3_prepare, sqlite3_step, etc. with 
no problems.  Now I'm trying to integrate the Backup API.  When I call 
sqlite3_backup_init it throws this error: _/"library /__/
/__/routine called out of sequence"/_.  I want to step into the Sqlite 
code to find out why but for some reason my debugger won't allow me to 
step into the C code from my C++ code.  So I'm hoping someone can help 
me.  Below is my C++ implementation which I modeled from the sample on 
this page: sqlite.org/backup.html.  My calling code is from a VB6 
project which simply creates/opens a memory DB and calls the below 
function (via my COM DLL).  Any help is much appreciated.


Thanks,
BrettG


SQLITE_DLL_API int backup( /*sqlite3*/ void* pdb, const WCHAR 
*pFilename, BOOL toFile )

{
  int rc;
  sqlite3 *pFileDb;
  sqlite3_backup *pBackup;
  sqlite3 *pTo;
  sqlite3 *pFrom;

  CTextConverter c(CP_UTF8);
  //rc = sqlite3_open_v2( c.U2M(pFilename), , 
SQLITE_OPEN_READWRITE, NULL);

  rc = sqlite3_open( c.U2M(pFilename), );

  if( rc==SQLITE_OK )
  {
try
{
sqlite3 *pMemoryDb = (sqlite3*) pdb;
pFrom = (toFile ? pMemoryDb : pFileDb);
pTo   = (toFile ? pFileDb : pMemoryDb);

// this line throws exception:  "library routine called out of 
sequence"

*pBackup = sqlite3_backup_init(pTo, "main", pFrom, "temp");*
if( pBackup )
{
sqlite3_backup_step(pBackup, -1);
sqlite3_backup_finish(pBackup);
}
rc = sqlite3_errcode(pTo);
sqlite3_close(pFileDb);
}
catch(...)
{
sqlite3_close(pFileDb);
const char* err = sqlite3_errmsg( (sqlite3*) pdb );
throw err;
}
  }
  return rc;
}

___
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users