Berkeley DB的内存泄露问题

#include "../include/db.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* Open a database */   
int open_db(DB **dbpp, const char *file_name, int is_secondary)
{       
    DB *dbp;
    u_int32_t   open_flags;
    int ret;
        
    /* Initialize the DB handle */
    ret = db_create(&dbp, NULL, 0);
    if(ret != 0)
        return ret;

    /* Point to the memory malloc'd by db_create() */
    *dbpp = dbp;

    /* Set the open flags */
    open_flags = DB_CREATE;

    /*
     * sorted duplicates.
     */
    if(is_secondary) {
        ret = dbp->set_flags(dbp, DB_DUP);
        if(ret != 0)
            return ret;
    }

    /* Now open the database */
    ret = dbp->open(dbp,          /* Pointer to the database */
                    NULL,                       /* Txn pointer */
                    file_name,          /* File name */
                    NULL,                       /* Logical db name */
                    DB_BTREE,                   /* Database type (using btree) */
                    open_flags,                 /* Open flags */
                    0);                         /* File mode. Using defaults */

    if(ret != 0)
        return ret;

    return 0;
}

int  main()
{
        char keybuff[1024*100];
        char valuebuff[1024*100];
        DBT         key, value;
        int         ret = -1;
        char       *buffer;
        bzero(&key, sizeof(DBT));
        bzero(&value, sizeof(DBT));

        DB* dbp;
        printf("open db\n");
        open_db(&dbp,"new-ec-rule.db", 1);

        DBC             *dbc;

        dbp->cursor(dbp, NULL, &dbc, 0);

        char* first=NULL;


       //下面6行如果删除,则有内存泄露,因为db会自己malloc内存,但是这块内存不能直接free。

        key.flags = DB_DBT_USERMEM;
        key.data = keybuff;
        key.ulen = 1024*100;
 
        value.flags = DB_DBT_USERMEM;

        value.data = valuebuff;

        value.ulen = 1024*100;      

        while( (ret = dbc->get(dbc, &key, &value, DB_NEXT)) == 0) {

                buffer = (char *)value.data;
                printf("buffer addr :%d\n",(int)buffer);
                printf("buffer size :%d\n",value.size);
        }
        dbp->err(dbp, ret, "DB->get");
        return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值