NO

Author Topic: Access violation error using sqlite3 functions  (Read 12811 times)

cfmspavia

  • Guest
Access violation error using sqlite3 functions
« on: January 26, 2009, 02:59:20 PM »
Sorry if this topic is duplicated, but I had some problems with the size of the attachments that exceded the 256k limitation of the forum, so I'm posting again.

I’m using Pelles C to write some functions to “wrap” the Sqlite3 API, but I’m having some problems. When calling sqlite3_open or sqlite3_close from within my function, I get an access violation error when my function returns. Using the sqlite3 functions directly there is no error. The attached project contains a small Win32 test program that demonstrates the problem.

SQLite version is 3.6.10

I’ve wrote similar functions in Windows CE and console apps and they work just fine, aparently at least.

Please, can anyone help me? Thanks.

Thanks Pelle, for your fine compiler.

p.s. The included zip file does not include the sqlite3 dll because of the 256k file size limitation of the forum. Sorry.

Carlos

JohnF

  • Guest
Re: Access violation error using sqlite3 functions
« Reply #1 on: January 28, 2009, 10:43:46 AM »
When passing a var to a function the value is passed, not the var. So your function SQLWR_OpenDB should be like this - passing the address

Code: [Select]
BOOL SQLWR_OpenDB (LPCSTR lpszDBFile, DBHANDLE * phDB)
{
BOOL r = sqlite3_open (lpszDBFile, phDB);
if (r != SQLITE_OK)
{
SQLWR_ShowSqlErrMB (*phDB);
}

return r;
} // ***** End SQLWR_OpenDB *****

The call should be like this - of course you may not need the MessageBox func.

Code: [Select]
case IDB_OPENDB: // Open database (wrapper function)
if(SQLWR_OpenDB (DBFILENAME, &ghDB) == SQLITE_OK)
MessageBox (NULL, TEXT ("Open DB Success!"), TEXT ("Test"), MB_OK |  MB_ICONINFORMATION);
else
MessageBox (NULL, TEXT ("Could not open DB!"), TEXT ("Test"), MB_OK |  MB_ICONINFORMATION);
return TRUE;

And the prototype should be like this

Code: [Select]
BOOL SQLWR_OpenDB (LPCSTR, DBHANDLE*);

Also look up WM_COMMAND - the return value should be zero if you handle the message.

John
« Last Edit: January 28, 2009, 11:21:42 AM by JohnF »

cfmspavia

  • Guest
Re: Access violation error using sqlite3 functions
« Reply #2 on: January 28, 2009, 01:12:13 PM »
John, thank you for your reply.

Unfortunately, even with your code the problem persists - Access violation when the SQLWR_OpenDB function returns.

I'm clueless

Thanks again

Carlos

JohnF

  • Guest
Re: Access violation error using sqlite3 functions
« Reply #3 on: January 28, 2009, 01:31:39 PM »
Try the attached file.

EDIT: I've just noticed that with 'maximize speed' it chashes, with optimizations as 'none' it doesn't.

John
« Last Edit: January 28, 2009, 01:42:12 PM by JohnF »

cfmspavia

  • Guest
Re: Access violation error using sqlite3 functions
« Reply #4 on: January 28, 2009, 01:54:34 PM »
John, thanks again for your patience.

Sorry, still same error, even with your file.

I must be doing something really stupid...

Carlos

JohnF

  • Guest
Re: Access violation error using sqlite3 functions
« Reply #5 on: January 28, 2009, 02:12:25 PM »
Did you try turning off optimization?

I've attached the project.

John


cfmspavia

  • Guest
Re: Access violation error using sqlite3 functions
« Reply #6 on: January 28, 2009, 02:19:13 PM »
John, a big thank you!

Turning off optimization solves the problem.

Carlos

JohnF

  • Guest
Re: Access violation error using sqlite3 functions
« Reply #7 on: January 28, 2009, 03:55:01 PM »
Yes, I don't know why that is a problem, maybe Pelle can help.

John

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
Re: Access violation error using sqlite3 functions
« Reply #8 on: January 28, 2009, 04:32:35 PM »
Maybe there are more things going on, but the calling convention matters. SQLite wants the __cdecl convention, so any calling program *must* use this too. This is inconvenient on Windows, where the __stdcall convention may be more appropriate.

Idea: maybe if the authors of sqlite added a macro (say SQLITE3_API) to function prototypes and callbacks in sqlite3.h, which could be "empty"/undefined by default, users on other platforms wouldn't be affected and users on Windows could define SQLITE3_API to __cdecl, or something...
/Pelle

JohnF

  • Guest
Re: Access violation error using sqlite3 functions
« Reply #9 on: January 28, 2009, 05:14:14 PM »
Yes, that fixed it, thanks.

John



cfmspavia

  • Guest
Re: Access violation error using sqlite3 functions
« Reply #10 on: January 28, 2009, 07:18:10 PM »
Worked for me. Modified sqlite3.h and was able to turn on optimizations again. Thanks Pelle.

That's why the WindowsCE and console apps worked fine with the same code. Both were using __cdecl and Win32 __stdcall. Stupid me... :-[

Carlos

Offline nsp

  • Member
  • *
  • Posts: 15
Re: Access violation error using sqlite3 functions
« Reply #11 on: January 28, 2009, 07:51:04 PM »
Yes, that fixed it, thanks.

Can you share this modified sqlite3.h ??

JohnF

  • Guest
Re: Access violation error using sqlite3 functions
« Reply #12 on: January 28, 2009, 08:26:58 PM »
Yes, that fixed it, thanks.

Can you share this modified sqlite3.h ??


I've not done the whole .h file - maybe Carlos has ?

However, this is the start.

Near the top put this

Code: [Select]
#if defined( _WIN32 )
 #define SQLITE3_API __cdecl
#else
 #define SQLITE3_API
#endif

then for each function prototype add the SQLITE3_API in the correct place.

e.g.

Code: [Select]
int SQLITE3_API sqlite3_open(
  const char *filename,   /* Database filename (UTF-8) */
  sqlite3 **ppDb          /* OUT: SQLite db handle */
);

John
« Last Edit: January 28, 2009, 08:29:52 PM by JohnF »

cfmspavia

  • Guest
Re: Access violation error using sqlite3 functions
« Reply #13 on: January 28, 2009, 08:44:29 PM »
Sorry, no. For the moment, just modified the include file in the 5 or 6 functions I am using in the test program.

Carlos

JohnF

  • Guest
Re: Access violation error using sqlite3 functions
« Reply #14 on: January 28, 2009, 09:56:18 PM »
Yes, that fixed it, thanks.

Can you share this modified sqlite3.h ??


Ok here it is, rushed.

I can't guarantee that it is complete or correct. Use at your own risk.

John