2 Replies Latest reply on Feb 28, 2012 7:39 AM by 919497

    Memory leak when using DB_DBT_MALLOC in CDB.

      Recently when I'm using Berkeley DB CDB and set the value Dbt DB_DBT_MALLOC, I noticed that the app's memory keeps growing during the db.get while;
      I found that many codes on the internet use the syntax free(value.get_data()), but when I tried to free the value.get_data(), I always got an interruption error.

      It's OK when I'm using the DB_DBT_USERMEM flag. So I just get very confused about the DB_DBT_MALLOC and DB_DBT_REALLOC.

      And also, I wonder why there's no memory leak problem when we use the single thread BDB. I think BDB cannot free the value.data pointer too in single thread before the app finish using it, then why our apps don't need to free the data afterwards?

      Thanks a lot in advance!
        • 1. Re: Memory leak when using DB_DBT_MALLOC in CDB.
          "Andrei Costache, Oracle-Oracle"
          You should use free() to free the DBT memory allocated via DB_DBT_MALLOC. You should do that after each Db::get() operation (and of course after finishing processing/using the data), otherwise you will loose the pointer to the memory previously allocated if you happen to reuse the DBT with DB_DBT_MALLOC (the data field will point to a new memory address).
          Alternatively you could use DB_DBT_REALLOC or DB_DBT_USERMEM.

          Note that there are small structures that BDB creates in the environment regions that only get freed/cleaned when the environment is closed.
          If you suspect that there is a memory leak inside the BDB code, make sure you rebuild Berkeley DB using the following configuration options (along with the others you use) when building: enable-debug, enable-umrw. Than, run the program under a memory leak detection utility, like Valgrind (allow the application to open and close the BDB environment) and see if there are any leaks reported.
          If memory leaks are reported, then put together a small stand-alone test case program that demonstrates the leaks, and post it here.

          1 person found this helpful
          • 2. Re: Memory leak when using DB_DBT_MALLOC in CDB.
            Hi, Andrei!

            Sorry about the title of my question. It's not BDB's problem of memory leak.

            I think I've found the problem, but I don't know how to resolve it. It may have something to do with the c runtime library.

            I am using vs2010 and the installation version BDB(db-5.1.25.msi). I link the libdb51.lib to my app. When I tried to free the DBT memory (under DB_DBT_MALLOC), my app got crash.

            As the web page below says, it's the multi-version CRTs that make my app crash. I think the BDB lib mallocs the DBT memory in a different heap againt my app, so when I free the memory in my app, it gets crash.
            I did as the page says, but it didn't work.

            Maybe I need to recompile BDB with some specific configuration so that my app and BDB lib can use the same CRT?