loudmouth/lm-connection.c
changeset 572 0a4fa0952ef6
parent 555 695c909d0c45
child 574 f84be3f7c7e0
equal deleted inserted replaced
567:0cb53b009e8c 572:0a4fa0952ef6
   189 static void
   189 static void
   190 connection_free (LmConnection *connection)
   190 connection_free (LmConnection *connection)
   191 {
   191 {
   192 	int        i;
   192 	int        i;
   193 
   193 
       
   194     /* This needs to be run before starting to free internal states.
       
   195      * It used to be run after the handlers where freed which lead to a crash
       
   196      * when the connection was freed prior to running lm_connection_close.
       
   197      */
       
   198     if (connection->state >= LM_CONNECTION_STATE_OPENING) {
       
   199 		connection_do_close (connection);
       
   200 	}
       
   201 
   194 	g_free (connection->server);
   202 	g_free (connection->server);
   195 	g_free (connection->jid);
   203 	g_free (connection->jid);
   196 	g_free (connection->effective_jid);
   204 	g_free (connection->effective_jid);
   197 	g_free (connection->stream_id);
   205 	g_free (connection->stream_id);
   198 	g_free (connection->resource);
   206 	g_free (connection->resource);
   199 
   207 
   200 	if (connection->sasl) {
   208   	if (connection->sasl) {
   201 		lm_sasl_free (connection->sasl);
   209 		lm_sasl_free (connection->sasl);
   202 	}
   210 	}
   203 
   211 
   204 	if (connection->parser) {
   212 	if (connection->parser) {
   205 		lm_parser_free (connection->parser);
   213 		lm_parser_free (connection->parser);
   216 			g_free (hd);
   224 			g_free (hd);
   217 		}
   225 		}
   218 
   226 
   219 		g_slist_free (connection->handlers[i]);
   227 		g_slist_free (connection->handlers[i]);
   220 	}
   228 	}
   221 
       
   222 	g_hash_table_destroy (connection->id_handlers);
   229 	g_hash_table_destroy (connection->id_handlers);
   223 	if (connection->state >= LM_CONNECTION_STATE_OPENING) {
   230 
   224 		connection_do_close (connection);
   231    	if (connection->open_cb) {
   225 	}
       
   226 
       
   227 	if (connection->open_cb) {
       
   228 		_lm_utils_free_callback (connection->open_cb);
   232 		_lm_utils_free_callback (connection->open_cb);
   229 	}
   233 	}
   230 	
   234 	
   231 	if (connection->auth_cb) {
   235 	if (connection->auth_cb) {
   232 		_lm_utils_free_callback (connection->auth_cb);
   236 		_lm_utils_free_callback (connection->auth_cb);
  2028 	
  2032 	
  2029 	g_return_if_fail (connection != NULL);
  2033 	g_return_if_fail (connection != NULL);
  2030 	g_return_if_fail (handler != NULL);
  2034 	g_return_if_fail (handler != NULL);
  2031 	g_return_if_fail (type != LM_MESSAGE_TYPE_UNKNOWN);
  2035 	g_return_if_fail (type != LM_MESSAGE_TYPE_UNKNOWN);
  2032 
  2036 
  2033 	for (l = connection->handlers[type]; l; l = l->next) {
  2037     for (l = connection->handlers[type]; l; l = l->next) {
  2034 		HandlerData *hd = (HandlerData *) l->data;
  2038         HandlerData *hd = (HandlerData *) l->data;
  2035 		
  2039 
  2036 		if (handler == hd->handler) {
  2040 		if (handler == hd->handler) {
  2037 			connection->handlers[type] = g_slist_remove_link (connection->handlers[type], l);
  2041             connection->handlers[type] = g_slist_remove_link (connection->handlers[type], l);
  2038 			g_slist_free (l);
  2042 			g_slist_free (l);
  2039 			lm_message_handler_unref (hd->handler);
  2043 			lm_message_handler_unref (hd->handler);
  2040 			g_free (hd);
  2044 			g_free (hd);
  2041 			break;
  2045 			break;
  2042 		}
  2046 		}
  2138 lm_connection_ref (LmConnection *connection)
  2142 lm_connection_ref (LmConnection *connection)
  2139 {
  2143 {
  2140 	g_return_val_if_fail (connection != NULL, NULL);
  2144 	g_return_val_if_fail (connection != NULL, NULL);
  2141 	
  2145 	
  2142 	connection->ref_count++;
  2146 	connection->ref_count++;
  2143 	
  2147     
  2144 	return connection;
  2148 	return connection;
  2145 }
  2149 }
  2146 
  2150 
  2147 /**
  2151 /**
  2148  * lm_connection_unref:
  2152  * lm_connection_unref:
  2155 lm_connection_unref (LmConnection *connection)
  2159 lm_connection_unref (LmConnection *connection)
  2156 {
  2160 {
  2157 	g_return_if_fail (connection != NULL);
  2161 	g_return_if_fail (connection != NULL);
  2158 	
  2162 	
  2159 	connection->ref_count--;
  2163 	connection->ref_count--;
  2160 	
  2164 
  2161 	if (connection->ref_count == 0) {
  2165 	if (connection->ref_count == 0) {
  2162 		connection_free (connection);
  2166         connection_free (connection);
  2163 	}
  2167 	}
  2164 }
  2168 }