Fixed most leaks and segfaults in ByteBuffer and ReferenceCounter.
Merged.
merged