Improvement to previous memory leak fix

Improved previous fix a bit and applied it to Decoder as well. Fixes a possible crash if Encoder::InitEncoder() wasn't called.