Work on bad accesses and then memory leaks.

Casting function pointers can be dangerous (since behavior is undefined in the specification), so I made a matching wrapper function.