Page 1 of 1

wxSQLite3 passing database pointer support?

Posted: Sat Dec 14, 2013 12:05 am
by ouch67
I'm getting problems when trying to the pointer of my already opened database to another window.

I'm just using similar code to this:

Code: Select all

wxSQLite3Database* newdb;
wxSQLite3Database olddb;

newdb = &olddb;

wxSQLite3Table ResultTable;
ResultTable = newdb->GetTable("select Name from TABLE;");
I get a crash on the GetTable line. (i've ran the sql command through the sqlite3 utility and it works so that's not it)

Is passing the pointer to a database is not allowed? Or am I just doing it wrong.

Re: wxSQLite3 passing database pointer support?

Posted: Sat Dec 14, 2013 12:52 pm
by utelle
ouch67 wrote:I'm getting problems when trying to the pointer of my already opened database to another window.
In principle this should not impose a problem. In my own application I quite often use a global wxSQLite3 database instance like in the following code snippet:

Code: Select all

wxSQLite3Database* Resources::GetDatabase()
{
  if (ms_db == NULL)
  {
    static wxSQLite3Database db;
    ms_db = &db;
  }
  return ms_db;
}
The only thing you have to keep in mind is that this database instance should not be used from different threads. If you use it exclusively from the main thread of a wxWidgets application it should work like a charm.
ouch67 wrote:I'm just using similar code to this:

Code: Select all

wxSQLite3Database* newdb;
wxSQLite3Database olddb;

newdb = &olddb;

wxSQLite3Table ResultTable;
ResultTable = newdb->GetTable("select Name from TABLE;");
I get a crash on the GetTable line. (i've ran the sql command through the sqlite3 utility and it works so that's not it)

Is passing the pointer to a database is not allowed? Or am I just doing it wrong.
From the code fragment you showed it's not clear whether your database pointer is valid or not. Also not clear is whether your application crashes due to an access violation (i.e. null pointer) or due to missing exception handling - although I would assume the latter.

wxSQLite3 throws an exception if the internal database pointer is invalid or if the table can't be fetched. Please surround your call to GetTable by a try-catch block and check/display the error message of the wxSQLite3Exception. This should give you a hint what's going wrong.

Regards,

Ulrich

Re: wxSQLite3 passing database pointer support?

Posted: Mon Dec 16, 2013 11:20 pm
by ouch67
What is the best way to catch an wxSQLite3 exception?

I tried:

Code: Select all

    try
    {
        ResultTable = dbs->GetTable("select Name from TABLE;");
    }
    catch(int e)
    {
        wxMessageBox(wxSQLite3Exception::ErrorCodeAsString(e));
    }
But no error message came up so I assume nothing was thrown?

Re: wxSQLite3 passing database pointer support?

Posted: Tue Dec 17, 2013 7:13 am
by utelle
ouch67 wrote:What is the best way to catch an wxSQLite3 exception?

I tried:

Code: Select all

    try
    {
        ResultTable = dbs->GetTable("select Name from TABLE;");
    }
    catch(int e)
    {
        wxMessageBox(wxSQLite3Exception::ErrorCodeAsString(e));
    }
But no error message came up so I assume nothing was thrown?
How is this supposed to work? Of course you have to catch not int e but wxSQLite3Exception e:

Code: Select all

  try
  {
    ResultTable = dbs->GetTable("select Name from TABLE;");
  }
  catch (wxSQLite3Exception& e)
  {
    wxMessageBox(e.GetMessage());
  }
wxSQLite3 includes a minimal sample showing such things.

Regards,

Ulrich

Re: wxSQLite3 passing database pointer support?

Posted: Tue Dec 17, 2013 8:24 pm
by ouch67
Well I tried your code and I don't see a message box either, so apparently it's crashing before wxSqlite3 takes over but the crash is on that line. Hmm...

Re: wxSQLite3 passing database pointer support?

Posted: Tue Dec 17, 2013 9:13 pm
by doublemax
Are you sure the pointer is valid? Maybe show some real code.

Re: wxSQLite3 passing database pointer support?

Posted: Tue Dec 17, 2013 9:25 pm
by utelle
ouch67 wrote:Well I tried your code and I don't see a message box either, so apparently it's crashing before wxSqlite3 takes over but the crash is on that line. Hmm...
The code of the method GetTable takes measures to not access a null pointer. The status of the database is checked internally, before calling the respective SQLite function. That is, there is no obvious reason that wxSQLite3 could crash within method GetTable.

Have you checked that your database pointer is valid before calling the GetTable method? You wouldn't see a wxSQLite3Exception thrown if the database pointer is invalid, because your application would crash on calling the method.

Another possibility would be that calling the respective SQLite function fails. This could happen if you use a SQLite DLL which does not contain the function sqlite3_get_table. Unlikely, but not impossible.

Please use a debugger to step through your application to verify that your database pointer is valid and that you can step into method GetTable and determine at which line within wxSQLite3 your application crashes, if you can step into GetTable.

Do you get any error messages when your application crashes? If yes, tell which ones.

For testing you could issue a GetTable call immediately after you opened the database, that is, before passing any pointers around. Does this work? Or does it crash, too?

Last but not least, describe your environment: operating system, wxWidgets version, compiler ...

Regards,

Ulrich

Re: wxSQLite3 passing database pointer support?

Posted: Wed Dec 18, 2013 11:41 pm
by ouch67
I got it figured out. Turns out it's a good idea to make sure your passing your pointers to a valid variable... lol

Basically there was a public variable in a class that was supposed to receive the pointer but it wasn't fully initialized by the time the database pointer was sent to it.

Thanks for the help though! I think I'll sprinkle that try-catch block code throughout my program just in case.