core: check for loop._ptr being NULL

raise ValueError if it is. Previously it crashed.

Closes #251. Thanks to Jan-Philip Gehrcke.