diff --git a/coreapi/linphonecore.c b/coreapi/linphonecore.c index bd852968e..bc52d67fa 100644 --- a/coreapi/linphonecore.c +++ b/coreapi/linphonecore.c @@ -1304,7 +1304,7 @@ static void linphone_core_init(LinphoneCore * lc, const LinphoneCoreVTable *vtab const char *remote_provisioning_uri = NULL; ms_message("Initializing LinphoneCore %s", linphone_core_get_version()); memset (lc, 0, sizeof (LinphoneCore)); - lc->config=config; + lc->config=lp_config_ref(config); lc->data=userdata; lc->ringstream_autorelease=TRUE; @@ -1421,8 +1421,11 @@ static void linphone_core_init(LinphoneCore * lc, const LinphoneCoreVTable *vtab LinphoneCore *linphone_core_new(const LinphoneCoreVTable *vtable, const char *config_path, const char *factory_config_path, void * userdata) { + LinphoneCore *lc; LpConfig *config = lp_config_new_with_factory(config_path, factory_config_path); - return linphone_core_new_with_config(vtable, config, userdata); + lc=linphone_core_new_with_config(vtable, config, userdata); + lp_config_unref(config); + return lc; } LinphoneCore *linphone_core_new_with_config(const LinphoneCoreVTable *vtable, struct _LpConfig *config, void *userdata) diff --git a/coreapi/lpconfig.c b/coreapi/lpconfig.c index 6e51d257c..d6ccea612 100644 --- a/coreapi/lpconfig.c +++ b/coreapi/lpconfig.c @@ -63,6 +63,7 @@ typedef struct _LpSection{ } LpSection; struct _LpConfig{ + int refcnt; FILE *file; char *filename; MSList *sections; @@ -287,6 +288,7 @@ LpConfig * lp_config_new(const char *filename){ LpConfig * lp_config_new_from_buffer(const char *buffer){ LpConfig* conf = lp_new0(LpConfig,1); + conf->refcnt=1; LpSection* current_section = NULL; char* ptr = ms_strdup(buffer); @@ -305,7 +307,7 @@ LpConfig * lp_config_new_from_buffer(const char *buffer){ LpConfig *lp_config_new_with_factory(const char *config_filename, const char *factory_config_filename) { LpConfig *lpconfig=lp_new0(LpConfig,1); - + lpconfig->refcnt=1; if (config_filename!=NULL){ ms_message("Using (r/w) config information from %s", config_filename); lpconfig->filename=ortp_strdup(config_filename); @@ -354,13 +356,28 @@ void lp_item_set_value(LpItem *item, const char *value){ } -void lp_config_destroy(LpConfig *lpconfig){ +static void _lp_config_destroy(LpConfig *lpconfig){ if (lpconfig->filename!=NULL) ortp_free(lpconfig->filename); ms_list_for_each(lpconfig->sections,(void (*)(void*))lp_section_destroy); ms_list_free(lpconfig->sections); free(lpconfig); } +LpConfig *lp_config_ref(LpConfig *lpconfig){ + lpconfig->refcnt++; + return lpconfig; +} + +void lp_config_unref(LpConfig *lpconfig){ + lpconfig->refcnt--; + if (lpconfig->refcnt==0) + _lp_config_destroy(lpconfig); +} + +void lp_config_destroy(LpConfig *lpconfig){ + lp_config_unref(lpconfig); +} + void lp_section_remove_item(LpSection *sec, LpItem *item){ sec->items=ms_list_remove(sec->items,(void *)item); lp_item_destroy(item); diff --git a/coreapi/lpconfig.h b/coreapi/lpconfig.h index f0f330c18..bd229803e 100644 --- a/coreapi/lpconfig.h +++ b/coreapi/lpconfig.h @@ -57,7 +57,7 @@ extern "C" { /** * Instantiates a LpConfig object from a user config file. - * + * The caller of this constructor owns a reference. lp_config_unref() must be called when this object is no longer needed. * @ingroup misc * @param filename the filename of the config file to read to fill the instantiated LpConfig * @see lp_config_new_with_factory @@ -66,7 +66,7 @@ LINPHONE_PUBLIC LpConfig * lp_config_new(const char *filename); /** * Instantiates a LpConfig object from a user provided buffer. - * + * The caller of this constructor owns a reference. lp_config_unref() must be called when this object is no longer needed. * @ingroup misc * @param buffer the buffer from which the lpconfig will be retrieved. We expect the buffer to be null-terminated. * @see lp_config_new_with_factory @@ -76,7 +76,7 @@ LINPHONE_PUBLIC LpConfig * lp_config_new_from_buffer(const char *buffer); /** * Instantiates a LpConfig object from a user config file and a factory config file. - * + * The caller of this constructor owns a reference. lp_config_unref() must be called when this object is no longer needed. * @ingroup misc * @param config_filename the filename of the user config file to read to fill the instantiated LpConfig * @param factory_config_filename the filename of the factory config file to read to fill the instantiated LpConfig @@ -259,6 +259,19 @@ LINPHONE_PUBLIC const char* lp_config_get_default_string(const LpConfig *lpconfi **/ LINPHONE_PUBLIC const char* lp_config_get_section_param_string(const LpConfig *lpconfig, const char *section, const char *key, const char *default_value); + +/** + * increment reference count + * @ingroup misc +**/ +LINPHONE_PUBLIC LpConfig *lp_config_ref(LpConfig *lpconfig); + +/** + * Decrement reference count, which will eventually free the object. + * @ingroup misc +**/ +LINPHONE_PUBLIC void lp_config_unref(LpConfig *lpconfig); + #ifdef __cplusplus } #endif