Small changes suggested by Konrad.

Switched from AllocCoTaskMem to AllocHGlobal.
Locking context counter.
Freeing automatically allocated pointers.