core: check for loop._ptr being NULL

raise ValueError if it is (previously it would crash).

Closes #251. Thanks to Jan-Philip Gehrcke.