diff -up a/PAMmodule.c b/PAMmodule.c
--- a/PAMmodule.c
+++ b/PAMmodule.c
@@ -538,10 +538,11 @@ static void PyPAM_dealloc(PyPAMObject *s
     free(self->service);
     free(self->user);
     free(self->conv);
-    pam_end(self->pamh, PAM_SUCCESS);
+    if (self->pamh)
+        pam_end(self->pamh, PAM_SUCCESS);
     dlclose(self->dlh2);
     dlclose(self->dlh1);
-    PyMem_DEL(self);
+    PyObject_Del(self);
 }
 
 static PyObject * PyPAM_getattr(PyPAMObject *self, char *name)
