In the following program, I
but it raises an segmentation fault.
Program received signal SIGSEGV, Segmentation fault.
0x00000000004008e4 in main (argc=2, argv=0x7fffffffe888) at testbdb.c:38
38 if ((ret = dbp1->open(dbp1,
why ?
code:
#include <stdio.h>
#include <stdlib.h>
#include <db.h>
int main(int argc,char** argv)
{
DB_ENV *dbenv=NULL;
DB *dbp1=NULL;
int ret;
int i;
if(argc!=2) return -1;
if ((ret = db_env_create(&dbenv, 0)) != 0) {
fprintf(stderr, "%s: %s\n", argv[0], db_strerror(ret));
exit(-1);
}
if ((ret =
dbenv->open(dbenv, argv[1], DB_CREATE | DB_INIT_MPOOL, 0)) != 0) {
dbenv->err(dbenv, ret, "environment open: %s", argv[1]);
exit(-1);
}
if ((ret = db_create(&dbp1, dbenv, 0)) != 0) {
dbenv->err(dbenv, ret, "database create");
exit(-1);
}
for( i=0;i<2;++i)
{
printf("open i=%d\n",i);
if ((ret = dbp1->open(dbp1,
NULL,
"database1", "database1",
(i==0?DB_BTREE:DB_UNKNOWN),
(i==0?DB_CREATE:DB_RDONLY),
0)) != 0) {
dbenv->err(dbenv, ret, "DB->open: database1");
exit(-1);
}
dbp1->close(dbp1, 0);
printf("close i=%d\n",i);
}
(void)dbenv->close(dbenv, 0);
return 0;
}
According to the documentation of the close() function "The DB handle may not be accessed again after DB->close() is called" which is what your loop is doing. That's probably a good place to start looking.