Re #7876. Fix potential memory leaks.
Re #7876. Use vectors instead of arrays to avoid leaks.
Re #7876. Fix memory leaks.

Don't create objects on the heap if you don't need to!