--- asterisk-11.15.0-broken/main/rtp_engine.c	2014-06-30 23:42:18.000000000 +0400
+++ asterisk-11.15.0/main/rtp_engine.c	2014-12-30 13:12:05.156308141 +0300
@@ -209,19 +209,21 @@
 	struct ast_rtp_instance *instance = obj;
 
 	/* Pass us off to the engine to destroy */
-	if (instance->data && instance->engine->destroy(instance)) {
+	if (instance && instance->engine && instance->data && instance->engine->destroy(instance)) {
 		ast_debug(1, "Engine '%s' failed to destroy RTP instance '%p'\n", instance->engine->name, instance);
 		return;
 	}
 
-	if (instance->srtp) {
+	if (instance && instance->srtp) {
 		res_srtp->destroy(instance->srtp);
 	}
 
 	ast_rtp_codecs_payloads_destroy(&instance->codecs);
 
 	/* Drop our engine reference */
-	ast_module_unref(instance->engine->mod);
+	if (instance && instance->engine){
+		ast_module_unref(instance->engine->mod);
+	}
 
 	ast_debug(1, "Destroyed RTP instance '%p'\n", instance);
 }
@@ -306,17 +308,23 @@
 
 int ast_rtp_instance_write(struct ast_rtp_instance *instance, struct ast_frame *frame)
 {
+	if (!instance || !instance->engine)
+		return -1;
+	
 	return instance->engine->write(instance, frame);
 }
 
 struct ast_frame *ast_rtp_instance_read(struct ast_rtp_instance *instance, int rtcp)
 {
-	return instance->engine->read(instance, rtcp);
+	return !instance ? NULL : !instance->engine ? NULL : instance->engine->read(instance, rtcp);
 }
 
 int ast_rtp_instance_set_local_address(struct ast_rtp_instance *instance,
 		const struct ast_sockaddr *address)
 {
+	if (!instance){
+		return 0;
+	}
 	ast_sockaddr_copy(&instance->local_address, address);
 	return 0;
 }
@@ -324,11 +332,15 @@
 int ast_rtp_instance_set_remote_address(struct ast_rtp_instance *instance,
 		const struct ast_sockaddr *address)
 {
+	if (!instance){
+		return 0;
+	}
+	
 	ast_sockaddr_copy(&instance->remote_address, address);
 
 	/* moo */
 
-	if (instance->engine->remote_address_set) {
+	if (instance->engine && instance->engine->remote_address_set) {
 		instance->engine->remote_address_set(instance, &instance->remote_address);
 	}
 
@@ -338,11 +350,14 @@
 int ast_rtp_instance_set_alt_remote_address(struct ast_rtp_instance *instance,
 		const struct ast_sockaddr *address)
 {
+	if (!instance){
+		return 0;
+	}
 	ast_sockaddr_copy(&instance->alt_remote_address, address);
 
 	/* oink */
 
-	if (instance->engine->alt_remote_address_set) {
+	if (instance->engine && instance->engine->alt_remote_address_set) {
 		instance->engine->alt_remote_address_set(instance, &instance->alt_remote_address);
 	}
 
@@ -352,6 +367,9 @@
 int ast_rtp_instance_get_and_cmp_local_address(struct ast_rtp_instance *instance,
 		struct ast_sockaddr *address)
 {
+	if (!instance){
+		return 0;
+	}
 	if (ast_sockaddr_cmp(address, &instance->local_address) != 0) {
 		ast_sockaddr_copy(address, &instance->local_address);
 		return 1;
@@ -363,12 +381,18 @@
 void ast_rtp_instance_get_local_address(struct ast_rtp_instance *instance,
 		struct ast_sockaddr *address)
 {
+	if (!instance){
+		return;
+	}
 	ast_sockaddr_copy(address, &instance->local_address);
 }
 
 int ast_rtp_instance_get_and_cmp_remote_address(struct ast_rtp_instance *instance,
 		struct ast_sockaddr *address)
 {
+	if (!instance){
+		return 0;
+	}
 	if (ast_sockaddr_cmp(address, &instance->remote_address) != 0) {
 		ast_sockaddr_copy(address, &instance->remote_address);
 		return 1;
@@ -380,19 +404,22 @@
 void ast_rtp_instance_get_remote_address(struct ast_rtp_instance *instance,
 		struct ast_sockaddr *address)
 {
+	if (!instance){
+		return;
+	}
 	ast_sockaddr_copy(address, &instance->remote_address);
 }
 
 void ast_rtp_instance_set_extended_prop(struct ast_rtp_instance *instance, int property, void *value)
 {
-	if (instance->engine->extended_prop_set) {
+	if (instance && instance->engine && instance->engine->extended_prop_set) {
 		instance->engine->extended_prop_set(instance, property, value);
 	}
 }
 
 void *ast_rtp_instance_get_extended_prop(struct ast_rtp_instance *instance, int property)
 {
-	if (instance->engine->extended_prop_get) {
+	if (instance && instance->engine && instance->engine->extended_prop_get) {
 		return instance->engine->extended_prop_get(instance, property);
 	}
 
@@ -401,9 +428,12 @@
 
 void ast_rtp_instance_set_prop(struct ast_rtp_instance *instance, enum ast_rtp_property property, int value)
 {
+	if (!instance){
+		return;
+	}
 	instance->properties[property] = value;
 
-	if (instance->engine->prop_set) {
+	if (instance->engine && instance->engine->prop_set) {
 		instance->engine->prop_set(instance, property, value);
 	}
 }
@@ -875,64 +905,64 @@
 {
 	codecs->pref = *prefs;
 
-	if (instance && instance->engine->packetization_set) {
+	if (instance && instance->engine && instance->engine->packetization_set) {
 		instance->engine->packetization_set(instance, &instance->codecs.pref);
 	}
 }
 
 int ast_rtp_instance_dtmf_begin(struct ast_rtp_instance *instance, char digit)
 {
-	return instance->engine->dtmf_begin ? instance->engine->dtmf_begin(instance, digit) : -1;
+	return !instance ? -1 : !instance->engine ? -1 : instance->engine->dtmf_begin ? instance->engine->dtmf_begin(instance, digit) : -1;
 }
 
 int ast_rtp_instance_dtmf_end(struct ast_rtp_instance *instance, char digit)
 {
-	return instance->engine->dtmf_end ? instance->engine->dtmf_end(instance, digit) : -1;
+	return !instance ? -1 : !instance->engine ? -1 : instance->engine->dtmf_end ? instance->engine->dtmf_end(instance, digit) : -1;
 }
 int ast_rtp_instance_dtmf_end_with_duration(struct ast_rtp_instance *instance, char digit, unsigned int duration)
 {
-	return instance->engine->dtmf_end_with_duration ? instance->engine->dtmf_end_with_duration(instance, digit, duration) : -1;
+	return !instance ? -1 : !instance->engine ? -1 : instance->engine->dtmf_end_with_duration ? instance->engine->dtmf_end_with_duration(instance, digit, duration) : -1;
 }
 
 int ast_rtp_instance_dtmf_mode_set(struct ast_rtp_instance *instance, enum ast_rtp_dtmf_mode dtmf_mode)
 {
-	return (!instance->engine->dtmf_mode_set || instance->engine->dtmf_mode_set(instance, dtmf_mode)) ? -1 : 0;
+	return !instance ? 0 : !instance->engine ? 0 : (!instance->engine->dtmf_mode_set || instance->engine->dtmf_mode_set(instance, dtmf_mode)) ? -1 : 0;
 }
 
 enum ast_rtp_dtmf_mode ast_rtp_instance_dtmf_mode_get(struct ast_rtp_instance *instance)
 {
-	return instance->engine->dtmf_mode_get ? instance->engine->dtmf_mode_get(instance) : 0;
+	return !instance ? 0 : !instance->engine ? 0 :instance->engine->dtmf_mode_get ? instance->engine->dtmf_mode_get(instance) : 0;
 }
 
 void ast_rtp_instance_update_source(struct ast_rtp_instance *instance)
 {
-	if (instance->engine->update_source) {
+	if (instance && instance->engine && instance->engine->update_source) {
 		instance->engine->update_source(instance);
 	}
 }
 
 void ast_rtp_instance_change_source(struct ast_rtp_instance *instance)
 {
-	if (instance->engine->change_source) {
+	if (instance && instance->engine && instance->engine->change_source) {
 		instance->engine->change_source(instance);
 	}
 }
 
 int ast_rtp_instance_set_qos(struct ast_rtp_instance *instance, int tos, int cos, const char *desc)
 {
-	return instance->engine->qos ? instance->engine->qos(instance, tos, cos, desc) : -1;
+	return !instance ? -1 : !instance->engine ? -1 : instance->engine->qos ? instance->engine->qos(instance, tos, cos, desc) : -1;
 }
 
 void ast_rtp_instance_stop(struct ast_rtp_instance *instance)
 {
-	if (instance->engine->stop) {
+	if (instance && instance->engine && instance->engine->stop) {
 		instance->engine->stop(instance);
 	}
 }
 
 int ast_rtp_instance_fd(struct ast_rtp_instance *instance, int rtcp)
 {
-	return instance->engine->fd ? instance->engine->fd(instance, rtcp) : -1;
+	return !instance ? -1 : !instance->engine ? -1 : instance->engine->fd ? instance->engine->fd(instance, rtcp) : -1;
 }
 
 struct ast_rtp_glue *ast_rtp_instance_get_glue(const char *type)
@@ -1826,17 +1856,17 @@
 
 int ast_rtp_red_init(struct ast_rtp_instance *instance, int buffer_time, int *payloads, int generations)
 {
-	return instance->engine->red_init ? instance->engine->red_init(instance, buffer_time, payloads, generations) : -1;
+	return !instance ? -1 : !instance->engine ? -1 : instance->engine->red_init ? instance->engine->red_init(instance, buffer_time, payloads, generations) : -1;
 }
 
 int ast_rtp_red_buffer(struct ast_rtp_instance *instance, struct ast_frame *frame)
 {
-	return instance->engine->red_buffer ? instance->engine->red_buffer(instance, frame) : -1;
+	return !instance ? -1 : !instance->engine ? -1 : instance->engine->red_buffer ? instance->engine->red_buffer(instance, frame) : -1;
 }
 
 int ast_rtp_instance_get_stats(struct ast_rtp_instance *instance, struct ast_rtp_instance_stats *stats, enum ast_rtp_instance_stat stat)
 {
-	return instance->engine->get_stat ? instance->engine->get_stat(instance, stats, stat) : -1;
+	return !instance ? -1 : !instance->engine ? -1 : instance->engine->get_stat ? instance->engine->get_stat(instance, stats, stat) : -1;
 }
 
 char *ast_rtp_instance_get_quality(struct ast_rtp_instance *instance, enum ast_rtp_instance_stat_field field, char *buf, size_t size)
@@ -1915,12 +1945,12 @@
 
 int ast_rtp_instance_set_read_format(struct ast_rtp_instance *instance, struct ast_format *format)
 {
-	return instance->engine->set_read_format ? instance->engine->set_read_format(instance, format) : -1;
+	return !instance ? -1 : !instance->engine ? -1 : instance->engine->set_read_format ? instance->engine->set_read_format(instance, format) : -1;
 }
 
 int ast_rtp_instance_set_write_format(struct ast_rtp_instance *instance, struct ast_format *format)
 {
-	return instance->engine->set_write_format ? instance->engine->set_write_format(instance, format) : -1;
+	return !instance ? -1 : !instance->engine ? -1 : instance->engine->set_write_format ? instance->engine->set_write_format(instance, format) : -1;
 }
 
 int ast_rtp_instance_make_compatible(struct ast_channel *chan, struct ast_rtp_instance *instance, struct ast_channel *peer)
@@ -1929,7 +1959,7 @@
 	struct ast_rtp_instance *peer_instance = NULL;
 	int res = -1;
 
-	if (!instance->engine->make_compatible) {
+	if (!instance || !instance->engine || !instance->engine->make_compatible) {
 		return -1;
 	}
 
@@ -1965,7 +1995,7 @@
 
 void ast_rtp_instance_available_formats(struct ast_rtp_instance *instance, struct ast_format_cap *to_endpoint, struct ast_format_cap *to_asterisk, struct ast_format_cap *result)
 {
-	if (instance->engine->available_formats) {
+	if (instance && instance->engine && instance->engine->available_formats) {
 		instance->engine->available_formats(instance, to_endpoint, to_asterisk, result);
 		if (!ast_format_cap_is_empty(result)) {
 			return;
@@ -1977,14 +2007,14 @@
 
 int ast_rtp_instance_activate(struct ast_rtp_instance *instance)
 {
-	return instance->engine->activate ? instance->engine->activate(instance) : 0;
+	return !instance ? 0 : !instance->engine ? 0 : instance->engine->activate ? instance->engine->activate(instance) : 0;
 }
 
 void ast_rtp_instance_stun_request(struct ast_rtp_instance *instance,
 				   struct ast_sockaddr *suggestion,
 				   const char *username)
 {
-	if (instance->engine->stun_request) {
+	if (instance && instance->engine && instance->engine->stun_request) {
 		instance->engine->stun_request(instance, suggestion, username);
 	}
 }
@@ -2087,7 +2117,7 @@
 
 int ast_rtp_instance_sendcng(struct ast_rtp_instance *instance, int level)
 {
-	if (instance->engine->sendcng) {
+	if (instance && instance->engine && instance->engine->sendcng) {
 		return instance->engine->sendcng(instance, level);
 	}
 
@@ -2096,12 +2126,12 @@
 
 struct ast_rtp_engine_ice *ast_rtp_instance_get_ice(struct ast_rtp_instance *instance)
 {
-	return instance->engine->ice;
+	return !instance ? NULL : !instance->engine ? NULL : instance->engine->ice;
 }
 
 struct ast_rtp_engine_dtls *ast_rtp_instance_get_dtls(struct ast_rtp_instance *instance)
 {
-	return instance->engine->dtls;
+	return !instance ? NULL : !instance->engine ? NULL :  instance->engine->dtls;
 }
 
 int ast_rtp_dtls_cfg_parse(struct ast_rtp_dtls_cfg *dtls_cfg, const char *name, const char *value)
