Index: channels/chan_sip.c =================================================================== --- channels/chan_sip.c (revision 103196) +++ channels/chan_sip.c (working copy) @@ -3071,6 +3071,18 @@ if (dumphistory) sip_dump_history(p); + /* Unlink us from the owner if we have one */ + if (p->owner) { + if (lockowner) + ast_channel_lock(p->owner); + if (option_debug) + ast_log(LOG_DEBUG, "Detaching from %s\n", p->owner->name); + p->owner->tech_pvt = NULL; + if (lockowner) + ast_channel_unlock(p->owner); + } + + /* Once we're unlinked from the ast_channel, now we're free to start destroying things. */ if (p->options) free(p->options); @@ -3098,16 +3110,6 @@ ASTOBJ_UNREF(p->registry, sip_registry_destroy); } - /* Unlink us from the owner if we have one */ - if (p->owner) { - if (lockowner) - ast_channel_lock(p->owner); - if (option_debug) - ast_log(LOG_DEBUG, "Detaching from %s\n", p->owner->name); - p->owner->tech_pvt = NULL; - if (lockowner) - ast_channel_unlock(p->owner); - } /* Clear history */ if (p->history) { struct sip_history *hist;