Added tests to detect a memory leak.
Fixed a memory leak problem caused by thread-safe collections.