WIP: Memory checks

Closed Oleksandr Zenaiev requested to merge memory_checks into master

This MR is created to investigate/discuss some places in the code related to invalid memory treatment and/or memory leaks which might need to be improved, it is not supposed to be merged at the moment.

Merge request reports