sqlite with C++

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

sqlite with C++

Andreas Volz
Hello,

I used this minimal example code to test my sqlite connection. This
works without problems. But now I tried to move the 'db' variable into
the private member section of my 'Cache' class to access it from
various member functions. That's all. I would assume that this makes no
difference. But my app crash at sqlite3_open().

bool Cache::isUrlCached (const string &url, string &outFilename)
{
  sqlite3 *db; // moved into private member of Cache
  char *zErrMsg = 0;
  int rc;
  string dbName ("../data/cache.db");
 
  string query ("SELECT url FROM cache");
 
  rc = sqlite3_open(dbName.c_str (), &db); // -> crash
 
  if( rc )
  {
    fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
    sqlite3_close(db);
    exit(1);
  }
 
  rc = sqlite3_exec(db, query.c_str (), callback, 0, &zErrMsg);
 
  if( rc!=SQLITE_OK )
  {
    fprintf(stderr, "SQL error: %s\n", zErrMsg);
    sqlite3_free(zErrMsg);
  }
 
  sqlite3_close(db);
}

Any ideas why this happens and how to prevent it?

regards
Andreas

-----------------------------------------------------------------------------
To unsubscribe, send email to [hidden email]
-----------------------------------------------------------------------------

Reply | Threaded
Open this post in threaded view
|

Re: sqlite with C++

Andreas Volz
Am Sun, 25 Nov 2007 12:11:50 +0100 schrieb Andreas Volz:

> Hello,
>
> I used this minimal example code to test my sqlite connection. This
> works without problems. But now I tried to move the 'db' variable into
> the private member section of my 'Cache' class to access it from
> various member functions. That's all. I would assume that this makes
> no difference. But my app crash at sqlite3_open()

Seems as it was a strange memory bug here. Forget my question.

regards
Andreas

-----------------------------------------------------------------------------
To unsubscribe, send email to [hidden email]
-----------------------------------------------------------------------------

Reply | Threaded
Open this post in threaded view
|

Re: sqlite with C++

Clay Dowling

Andreas Volz wrote:

> Am Sun, 25 Nov 2007 12:11:50 +0100 schrieb Andreas Volz:
>
>> Hello,
>>
>> I used this minimal example code to test my sqlite connection. This
>> works without problems. But now I tried to move the 'db' variable into
>> the private member section of my 'Cache' class to access it from
>> various member functions. That's all. I would assume that this makes
>> no difference. But my app crash at sqlite3_open()
>
> Seems as it was a strange memory bug here. Forget my question.

Andreas,

While there might be a strange memory bug going on, the bigger issue is
that if you have a db member of the Cache class, you're declaring a local
db variable that's masking the member.  That could be leading to
unexpected results later on if you're expecting to use it.

Clay

--
Lazarus Registration
http://www.lazarusid.com/registration.shtml


-----------------------------------------------------------------------------
To unsubscribe, send email to [hidden email]
-----------------------------------------------------------------------------

Reply | Threaded
Open this post in threaded view
|

Re: sqlite with C++

Andreas Volz
Am Mon, 26 Nov 2007 08:36:47 -0500 (EST) schrieb Clay Dowling:

>
> Andreas Volz wrote:
> > Am Sun, 25 Nov 2007 12:11:50 +0100 schrieb Andreas Volz:
> >
> >> Hello,
> >>
> >> I used this minimal example code to test my sqlite connection. This
> >> works without problems. But now I tried to move the 'db' variable
> >> into the private member section of my 'Cache' class to access it
> >> from various member functions. That's all. I would assume that
> >> this makes no difference. But my app crash at sqlite3_open()
> >
> > Seems as it was a strange memory bug here. Forget my question.
>
> Andreas,
>
> While there might be a strange memory bug going on, the bigger issue
> is that if you have a db member of the Cache class, you're declaring
> a local db variable that's masking the member.  That could be leading
> to unexpected results later on if you're expecting to use it.

This was only example code. I commented that out in if I use it as
member variable. But I found the bug. It was because of wrong
initialization of a global variable and crashed because of side effects.

regards
Andreas

-----------------------------------------------------------------------------
To unsubscribe, send email to [hidden email]
-----------------------------------------------------------------------------