examples: Fix possible division by zero
Fix resource leaks