This content has been marked as final. Show 2 replies
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).1 person found this helpful
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.
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?