test.c的:的ctypes和內存泄漏
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#define MAXFRAGMENTS 4000000
typedef struct {
uint64_t FirstFragmentTimestamp;
} fragment_t;
typedef struct {
fragment_t** fragments;
} bts_t;
bts_t* initialize() {
uint32_t i;
bts_t* bts;
bts = (bts_t *) malloc(sizeof(bts_t));
bts->fragments= (fragment_t **) malloc(sizeof(bts_t *)*MAXFRAGMENTS);
for(i=0;i<MAXFRAGMENTS;i++) {
(bts->fragments)[i]=(fragment_t *) malloc(sizeof(fragment_t));
(bts->fragments)[i]->FirstFragmentTimestamp = 0;
}
return bts;
}
int fr(bts_t *bts)
{
uint32_t i;
if (bts != NULL) {
for(i=0;i<MAXFRAGMENTS;i++) {
free(bts -> fragments[i]);
}
free(bts->fragments);
}
free(bts);
return 1;
}
int main() {
}
test.py:
from ctypes import *
import time
class fragment(Structure):
_fields_=[("FirstFragmentTimestamp",c_ulong)]
class bts(Structure):
_fields_=[("fragments",POINTER(POINTER(fragment)))]
bts_pointer=POINTER(bts)
bts_library=CDLL("test.so")
bts_initialize = bts_library.initialize
bts_initialize.restype = bts_pointer
bts_free = bts_library.fr
m = bts_pointer()
m = bts_initialize()
bts_free(m)
print 'done'
time.sleep(20)
爲什麼狀元秀,什麼內存不是自由運行bts_free後腳本結束之前?
這是不對的。至少,他們主要有能力這樣做。如果我'malloc()'一個巨大的部分和'free()'它再次,我的進程的大小縮小。 – glglgl