Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8936971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:19:39+00:00 2026-06-15T10:19:39+00:00

I have a problem with deallocating memory after calling DB->put(). It says double free

  • 0

I have a problem with deallocating memory after calling DB->put().
It says “double free or corruption”, when I do it.

Code sample:

DBT key,value;
int err;
...
memset(value,0,sizeof(DBT));
value.data=malloc(10);
memset(value.data,10);
value.flags=DB_DBT_MALLOC;
...
value.size=10;
...
if((err=db->put(db,NULL,&key,&value,0))){
    ...
}
free(value.data);

The C API reference does not contain any information on the subject, it tells only some information about using DB_DBT_MALLOC when retrieving (but not storing) data:

When this flag is set, Berkeley DB will allocate memory for the returned key or data item (using malloc(3), or the user-specified malloc function), and return a pointer to it in the data field of the key or data DBT structure. Because any allocated memory becomes the responsibility of the calling application, the caller must determine whether memory was allocated using the returned value of the data field.

When DB->put is called, Berkeley DB does NOT return any data, it just stores a key/data pair. Does it copy the data, or should the memory be alife until DB->close or ENV->close is called?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T10:19:40+00:00Added an answer on June 15, 2026 at 10:19 am

    DB_DBT_MALLOC has no effect in db->put() since db->put() will copy the data. So you are most likely corrupting your heap elsewhere. This kind of error doesn’t always manifest itself where they were caused. My guess is that you’re writing off the end of some buffer. Try running Valgrind on it, it’s often very useful to track this kind of issue down.

    I ran this simple test program and it works just fine and valgrind reports no leaks:

    void test() {
        DB *DB;
        DBT key;
        DBT data;
        DBT value;
        int i;
    
        db_create(&DB, NULL, 0); 
        if(DB->open(DB, NULL, "tmp.db2", NULL, DB_BTREE, DB_CREATE, 0664) != 0) {
            printf("DB->open failed!\n");
            exit(0);
        }
    
        memset(&value,0,sizeof(value));
        value.size=10;
        value.data=malloc(value.size);
        memset(&key,0,sizeof(key));
        key.data=malloc(sizeof(unsigned int));
        key.size = sizeof(unsigned int);    
        for(i=0; i < 10; i++) {
            *(unsigned int*)key.data = i;
                memset(value.data, 0, value.size); /* valgrind complains if I leave this out? */
            sprintf(value.data, "test %d", i);
    
            if(DB->put(DB, NULL, &key, &value, 0 ) != 0) {
                printf("key stored failed\n");
                exit(-1);
            }
        }
        free(value.data);
        free(key.data);
    
        key.data = malloc(sizeof(unsigned int));
        key.size = sizeof(unsigned int); 
        *(unsigned int*)key.data=5;
        memset(&data,0,sizeof(data));
        data.flags = DB_DBT_MALLOC;
        DB->get(DB, NULL, &key,&data, 0);
    
        printf("size: %d data: %s\n", data.size, (const char*)data.data);
        DB->close(DB, 0);
        free(data.data);
        free(key.data);
    
        return;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm getting a weird problem in memory deallocation. I have the following code for
I have problem with repopulating form_upload after validation. Other input fields or selectboxes are
I have trouble deallocating memory that I allocated using malloc. The program runs fine
I have a piece of C++ code that deallocates memory as follows for (int
I have problem with simple jquery sliding menu, here is my code: http://jsfiddle.net/kCK44/1/ what
I have problem with code below. Can some one tell why it doesn't work
I have problem with http://abfoodpolicy.com/ . In IE 8 and 9 the right sidebar
I have problem with my query on C, I’m using the oci8 driver. This
I have problem with show or hide form in Window Form Application. I start
I have problem with UIWebView delay when the load image from url. In my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.