diff --git a/main/srv.c b/main/srv.c
index f4e0ec39aa9b97d0f4bf8d2ab40518dd3ce23246..5a0c7effbfadb7188ec07fa71f0cd3d9c3accaf0 100644
--- a/main/srv.c
+++ b/main/srv.c
@@ -249,7 +249,11 @@ void ast_srv_cleanup(struct srv_context **context)
 	const char *host;
 	unsigned short port;
 
-	while (!(ast_srv_lookup(context, NULL, &host, &port)));
+	if (*context) {
+		/* We have a context to clean up. */
+		while (!(ast_srv_lookup(context, NULL, &host, &port))) {
+		}
+	}
 }
 
 int ast_get_srv(struct ast_channel *chan, char *host, int hostlen, int *port, const char *service)