Drop it!

The Drop trait is a very simple trait:

pub trait Drop  
{ 
    fn drop(&mut self); 
} 

As with all traits, it requires an impl for it before the trait can be used:

struct FreeMemory; 
impl Drop for FreeMemory 
{ 
     fn drop(&mut self); 
} 

At this point, we call our pub fn, which returns a data block from ImageMagick. Once we have done what we need to do with that memory block, we have to free it. We stored the data in a variable called graphics_block. To free the block from graphics_block, we use:

graphics_block  = FreeMemory; 

The memory is freed once graphics_block goes out of scope.

It is worth pointing out that panic! will call drop as it unwinds the memory. If you therefore have a panic! within a drop, chances are that it is going to abort.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset