Fix bugs of Windows port

Fixed:

* crash when running for some time.
* dead loop under some conditions.