Fixed memory leak

This commit is contained in:
Dennis Schwerdel 2017-03-23 15:58:49 +01:00
parent 0d94a75613
commit 8b859dddb9
1 changed files with 13 additions and 6 deletions

View File

@ -3,6 +3,7 @@ use std::ffi::{CStr, CString};
use std::io::{self, Write}; use std::io::{self, Write};
use std::str::FromStr; use std::str::FromStr;
use libc;
use squash::*; use squash::*;
@ -203,7 +204,7 @@ impl Compression {
if stream.is_null() { if stream.is_null() {
return Err(CompressionError::InitializeStream); return Err(CompressionError::InitializeStream);
} }
Ok(CompressionStream::new(unsafe { Box::from_raw(stream) })) Ok(CompressionStream::new(stream))
} }
#[inline] #[inline]
@ -215,19 +216,19 @@ impl Compression {
if stream.is_null() { if stream.is_null() {
return Err(CompressionError::InitializeStream); return Err(CompressionError::InitializeStream);
} }
Ok(CompressionStream::new(unsafe { Box::from_raw(stream) })) Ok(CompressionStream::new(stream))
} }
} }
pub struct CompressionStream { pub struct CompressionStream {
stream: Box<SquashStream>, stream: *mut SquashStream,
buffer: [u8; 16*1024] buffer: [u8; 16*1024]
} }
impl CompressionStream { impl CompressionStream {
#[inline] #[inline]
fn new(stream: Box<SquashStream>) -> Self { fn new(stream: *mut SquashStream) -> Self {
CompressionStream { CompressionStream {
stream: stream, stream: stream,
buffer: [0; 16*1024] buffer: [0; 16*1024]
@ -235,7 +236,7 @@ impl CompressionStream {
} }
pub fn process<W: Write>(&mut self, input: &[u8], output: &mut W) -> Result<(), CompressionError> { pub fn process<W: Write>(&mut self, input: &[u8], output: &mut W) -> Result<(), CompressionError> {
let mut stream = &mut *self.stream; let stream = unsafe { &mut (*self.stream) };
stream.next_in = input.as_ptr(); stream.next_in = input.as_ptr();
stream.avail_in = input.len(); stream.avail_in = input.len();
loop { loop {
@ -255,7 +256,7 @@ impl CompressionStream {
} }
pub fn finish<W: Write>(mut self, output: &mut W) -> Result<(), CompressionError> { pub fn finish<W: Write>(mut self, output: &mut W) -> Result<(), CompressionError> {
let mut stream = &mut *self.stream; let stream = unsafe { &mut (*self.stream) };
loop { loop {
stream.next_out = self.buffer.as_mut_ptr(); stream.next_out = self.buffer.as_mut_ptr();
stream.avail_out = self.buffer.len(); stream.avail_out = self.buffer.len();
@ -272,3 +273,9 @@ impl CompressionStream {
Ok(()) Ok(())
} }
} }
impl Drop for CompressionStream {
fn drop(&mut self) {
unsafe { squash_object_unref(self.stream as *mut libc::c_void); }
}
}