Pelles C forum

C language => Beginner questions => Topic started by: cfmspavia on January 26, 2009, 02:59:20 PM

Title: Access violation error using sqlite3 functions
Post by: cfmspavia 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
Title: Re: Access violation error using sqlite3 functions
Post by: JohnF 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
Title: Re: Access violation error using sqlite3 functions
Post by: cfmspavia 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
Title: Re: Access violation error using sqlite3 functions
Post by: JohnF 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
Title: Re: Access violation error using sqlite3 functions
Post by: cfmspavia 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
Title: Re: Access violation error using sqlite3 functions
Post by: JohnF on January 28, 2009, 02:12:25 PM
Did you try turning off optimization?

I've attached the project.

John

Title: Re: Access violation error using sqlite3 functions
Post by: cfmspavia on January 28, 2009, 02:19:13 PM
John, a big thank you!

Turning off optimization solves the problem.

Carlos
Title: Re: Access violation error using sqlite3 functions
Post by: JohnF on January 28, 2009, 03:55:01 PM
Yes, I don't know why that is a problem, maybe Pelle can help.

John
Title: Re: Access violation error using sqlite3 functions
Post by: Pelle 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...
Title: Re: Access violation error using sqlite3 functions
Post by: JohnF on January 28, 2009, 05:14:14 PM
Yes, that fixed it, thanks.

John


Title: Re: Access violation error using sqlite3 functions
Post by: cfmspavia 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
Title: Re: Access violation error using sqlite3 functions
Post by: nsp on January 28, 2009, 07:51:04 PM
Yes, that fixed it, thanks.

Can you share this modified sqlite3.h ??
Title: Re: Access violation error using sqlite3 functions
Post by: JohnF 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
Title: Re: Access violation error using sqlite3 functions
Post by: cfmspavia 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
Title: Re: Access violation error using sqlite3 functions
Post by: JohnF 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
Title: Re: Access violation error using sqlite3 functions
Post by: David L Morris on October 05, 2009, 11:29:45 AM
Hello to you I am a new user.  I downloaded the wrsqlit3 project files and was pleased to find the included exe file ran and worked as might be expected.  When I tried to compile it with my new Pelles C version 6.00.4, the compile does not work and displays the following:-

Building main.obj.
Building main.res.
Building wrsqlit3.exe.
POLINK: error: Unresolved external symbol '_sqlite3_open@8'.
POLINK: error: Unresolved external symbol '_sqlite3_close@4'.
POLINK: error: Unresolved external symbol '_sqlite3_malloc@4'.
POLINK: error: Unresolved external symbol '_sqlite3_exec@20'.
POLINK: error: Unresolved external symbol '_sqlite3_free@4'.
POLINK: error: Unresolved external symbol '_sqlite3_errcode@4'.
POLINK: error: Unresolved external symbol '_sqlite3_errmsg@4'.
POLINK: fatal error: 7 unresolved external(s).
*** Error code: 1 ***
Done.

In hope you might be able to help?  I have been using PowerBasic to successfully work on SQLite3. Now I am retired I want to relearn the  C language.

Thanks  David
Title: Re: Access violation error using sqlite3 functions
Post by: Stefan Pendl on October 05, 2009, 12:14:24 PM
There are several threads dealing with this problem, so please try searching for "sqlite" to get the resolution.
Title: Re: Access violation error using sqlite3 functions
Post by: David L Morris on October 05, 2009, 01:33:11 PM
Ok Stefan, my search for sqlite seems to only present this complete thread. I made changes to the Sqlite3.h file and it still doesn't compile but the errors showing are as below so I would appreciate any more guidance.

Building wrsqlit3.exe.
POLINK: error: Unresolved external symbol '_sqlite3_malloc@4'.
POLINK: error: Unresolved external symbol '_sqlite3_free@4'.
POLINK: error: Unresolved external symbol '_sqlite3_errmsg@4'.
POLINK: fatal error: 3 unresolved external(s).
*** Error code: 1 ***
Done.
Title: Re: Access violation error using sqlite3 functions
Post by: frankie on October 05, 2009, 08:34:57 PM
The error you get clearly declare that the missing functions are expected to be __stdcall (as highlited from the presence of decorations), but the sqlite3 libraries and DLL are __cdecl.
This means that you haven't modified correctly the sqlite3.h adding __cdecl attribute at each function declaration as explained in many threads on this issue (as Stefan said).
Please make the required corrections and also read better the threads so you will find also the sqlite3.h file correctly updated.
Title: Re: Access violation error using sqlite3 functions
Post by: David L Morris on October 05, 2009, 10:56:12 PM
Thanks frankie. I have just corrected what I think are the last changes and it compiles OK.  I added a word to the dialog caption and that shows up correctly in the compiled exe.  Now I can try some actual development, so no doubt I will be back for help as time proceeds.
Regards
David
Title: Re: Access violation error using sqlite3 functions
Post by: David L Morris on October 06, 2009, 12:09:57 AM
I see that for each project in pelles C, an Sqlite3 database file (*.tag) is created with a browse table in the project folder.  That is interesting to me and gives an endorsement to the quality of the Sqlite database. I would appreciate any comment on this?

David
Title: Re: Access violation error using sqlite3 functions
Post by: Robert on October 06, 2009, 06:43:46 AM
I see that for each project in pelles C, an Sqlite3 database file (*.tag) is created with a browse table in the project folder.  That is interesting to me and gives an endorsement to the quality of the Sqlite database. I would appreciate any comment on this?

David

The quality is obvious from the implicit endorsement by many famous users. For details please see

http://www.sqlite.org/famous.html

Robert Wishlaw