Memory leak when binding my rust lib to my c++ legacy

Hi,

I need some help. I do not want to smother anyone with my scribbling so I'll post fraction by fraction in this thread as you guys say it is necessary. The problem is a memory leak that apparently happens at ABI for my FFI where I am constructing an c++ object (which is basically an in memory db ) and try to repeatedly get a pattern from it. If i do it directly by making a small cli for my c++ object and make the same set of queries using only c++ env, I get no mem leaks but if i do the same through my ABI memory keeps rising. Code goes like this:

//--------------ABI mod.rs
#[repr(C)]
pub struct LztObj {
    _opaque: [*const u8; 0],
}


//-------------  FFI.rs

extern "C"{

    pub fn query_lzt (
        obj: *mut LztObj,
        vst: *const libc::c_uchar,
        vln: libc::c_ulong,
    )-> u64;
}


pub struct FFI {
    raw: Vec<*mut LztObj>
}

impl FFI{
    pub fn new() -> Self {
    .......
        FFI {
            raw: ....
        }
    }
}



pub fn get(&self, pattern: &str) -> Vec<u8>{
    let mut qres : Vec<u8> = Vec::new();
    for i in 0..self.raw.len() {  // a vector of object pointers 
        unsafe{
           let size = query_lzt(
                   self.raw[i],
                   pattern.as_ptr(),
                   pattern.len() as libc::c_ulong,
           ) as usize;

           let mut tq = vec![0u8;size];
           get_query_results(
                   self.raw[i],
                   tq.as_mut_ptr()
           );

           qres.extend(tq);
           }
        }
  qres      
 }
 
 
//-----------------and in my main.rs:
 
let mut obj = FFI::new();

loop {
 let mut res : Vec<u8> = obj.get(&"mypatt");  // MEM leak
}

obj.drop();  // implementation not sketched out 

if i comment out the line in main loop, memory is stable, however if i leave it as is, it leaks...

can anyone see why would there be a mem leak here or is this fraction of code insufficient ? What would be the best approach here to locate the leak?

Thank you !!

Is it possible that you didn't call a c++ destructor to release resources?

1 Like

Thnx! U were correct a very complicated destructor in my legacy code was not working properly. so this was not an ABI issue... Thnx once more :slight_smile: