Index: res/res_pjsip.c =================================================================== --- res/res_pjsip.c (revision 432991) +++ res/res_pjsip.c (working copy) @@ -3370,14 +3370,21 @@ static int unload_pjsip(void *data) { + ast_res_pjsip_cleanup_options_handling(); + internal_sip_destroy_outbound_authentication(); + ast_sip_destroy_distributor(); + ast_res_pjsip_destroy_configuration(); + ast_sip_destroy_system(); + ast_sip_destroy_global_headers(); + internal_sip_unregister_service(&supplement_module); + if (monitor_thread) { + stop_monitor_thread(); + } if (memory_pool) { pj_pool_release(memory_pool); memory_pool = NULL; } - if (ast_pjsip_endpoint) { - pjsip_endpt_destroy(ast_pjsip_endpoint); - ast_pjsip_endpoint = NULL; - } + ast_pjsip_endpoint = NULL; pj_caching_pool_destroy(&caching_pool); pj_shutdown(); return 0; @@ -3385,16 +3392,6 @@ static int unload_module(void) { - ast_res_pjsip_cleanup_options_handling(); - internal_sip_destroy_outbound_authentication(); - ast_sip_destroy_distributor(); - ast_res_pjsip_destroy_configuration(); - ast_sip_destroy_system(); - ast_sip_destroy_global_headers(); - internal_sip_unregister_service(&supplement_module); - if (monitor_thread) { - stop_monitor_thread(); - } /* The thread this is called from cannot call PJSIP/PJLIB functions, * so we have to push the work to the threadpool to handle */ Index: res/res_pjsip_keepalive.c =================================================================== --- res/res_pjsip_keepalive.c (revision 432991) +++ res/res_pjsip_keepalive.c (working copy) @@ -244,7 +244,7 @@ ast_sorcery_observer_add(ast_sip_get_sorcery(), "global", &keepalive_global_observer); ast_sorcery_reload_object(ast_sip_get_sorcery(), "global"); - ast_module_ref(ast_module_info->self); + ast_module_shutdown_ref(ast_module_info->self); return AST_MODULE_LOAD_SUCCESS; } Index: res/res_pjsip_notify.c =================================================================== --- res/res_pjsip_notify.c (revision 432991) +++ res/res_pjsip_notify.c (working copy) @@ -1021,6 +1021,7 @@ ast_manager_unregister("PJSIPNotify"); ast_cli_unregister_multiple(cli_options, ARRAY_LEN(cli_options)); aco_info_destroy(¬ify_cfg); + ao2_global_obj_release(globals); return 0; } Index: res/res_pjsip_outbound_registration.c =================================================================== --- res/res_pjsip_outbound_registration.c (revision 432991) +++ res/res_pjsip_outbound_registration.c (working copy) @@ -421,7 +421,6 @@ ast_log(LOG_WARNING, "Failed to pass outbound registration to threadpool\n"); ao2_ref(client_state, -1); } - ao2_ref(client_state, -1); entry->id = 0; } @@ -626,6 +625,7 @@ /* If deferred destruction is in use see if we need to destroy now */ if (response->client_state->destroy) { handle_client_state_destruction(response->client_state); + response->client_state = NULL; } return 0; Index: res/res_pjsip_pubsub.c =================================================================== --- res/res_pjsip_pubsub.c (revision 432991) +++ res/res_pjsip_pubsub.c (working copy) @@ -4286,6 +4286,7 @@ ast_manager_unregister(AMI_SHOW_SUBSCRIPTIONS_INBOUND); ast_manager_unregister("PJSIPShowResourceLists"); + ast_sip_unregister_service(&pubsub_module); if (sched) { ast_sched_context_destroy(sched); } Index: res/res_pjsip_session.c =================================================================== --- res/res_pjsip_session.c (revision 432991) +++ res/res_pjsip_session.c (working copy) @@ -2642,14 +2642,18 @@ } ast_sip_register_service(&session_reinvite_module); - ast_module_ref(ast_module_info->self); + ast_module_shutdown_ref(ast_module_info->self); return AST_MODULE_LOAD_SUCCESS; } static int unload_module(void) { - /* This will never get called as this module can't be unloaded */ + ast_sip_unregister_service(&session_reinvite_module); + ast_sip_unregister_service(&session_module); + ast_sorcery_delete(ast_sip_get_sorcery(), nat_hook); + ao2_cleanup(nat_hook); + ao2_cleanup(sdp_handlers); return 0; }