title: Re-Opening a BerkeleyDB database raises a Segmentation fault
body: <p>In the following program, I </p>
  
  <ul>
  <li>open a BDB environment </li>
  <li>open a database  DB_BTREE/DB_CREATE</li>
  <li>close the database</li>
  <li>reopen the database as DB_UNKNOWN/DB_RDONLY ...</li>
  </ul>
  
  <p>but it raises an segmentation fault.</p>
  
  <pre><code>Program received signal SIGSEGV, Segmentation fault.
  0x00000000004008e4 in main (argc=2, argv=0x7fffffffe888) at testbdb.c:38
  38                  if ((ret = dbp1-&gt;open(dbp1,
  </code></pre>
  
  <p>why ? </p>
  
  <p>code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;db.h&gt;
  
  
  
  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(&amp;dbenv, 0)) != 0) {
              fprintf(stderr, "%s: %s\n", argv[0], db_strerror(ret));
              exit(-1);
          }
  
  
          if ((ret =
              dbenv-&gt;open(dbenv, argv[1], DB_CREATE | DB_INIT_MPOOL, 0)) != 0) {
              dbenv-&gt;err(dbenv, ret, "environment open: %s", argv[1]);
              exit(-1);
          }
  
  
          if ((ret = db_create(&amp;dbp1, dbenv, 0)) != 0) {
              dbenv-&gt;err(dbenv, ret, "database create");
              exit(-1);
          }
          for( i=0;i&lt;2;++i)
                  {
                  printf("open i=%d\n",i);
                  if ((ret = dbp1-&gt;open(dbp1,
                          NULL,
                          "database1", "database1",
                          (i==0?DB_BTREE:DB_UNKNOWN),
                          (i==0?DB_CREATE:DB_RDONLY),
                          0)) != 0) {
                      dbenv-&gt;err(dbenv, ret, "DB-&gt;open: database1");
                       exit(-1);
                      }
  
                  dbp1-&gt;close(dbp1, 0);
                  printf("close i=%d\n",i);
                  }
  
  
      (void)dbenv-&gt;close(dbenv, 0);
      return 0;
      }
  </code></pre>