0

ライブラリを使用してlibconfig、ファイルから構成データを読み取っています。情報を解析して後でクリーンアップする関数を抽出するのに苦労しています。

実行strcpy(*hostname, tmp)すると、コア ダンプが発生します。

hostnameport、およびipは に初期化されNULLます。

int parseConfig(char **hostname, char **port, char **ip) {

    config_t cfg, *cf;
    const char *tmp;

    cf = &cfg;
    config_init(cf);

    if(!config_read_file(cf, CONFIG)) {
        fprintf(stderr, "%s:%d - %s\n",
            config_error_file(cf),
            config_error_line(cf),
            config_error_text(cf));
        config_destroy(cf);
        return(EXIT_FAILURE);
    }

    config_lookup_string(cf, "hostname",  &tmp);
    strcpy(*hostname, tmp);
    config_lookup_string(cf, "ip", &tmp);
    strcpy(*ip, tmp);
    config_lookup_string(cf, "port", &tmp);
    strcpy(*port, tmp);

    config_destroy(cf);

    return(EXIT_SUCCESS);
}
4

2 に答える 2

0

仲買人をカットします。

config_lookup_string(cf, "hostname",  hostname);

cfg構成文字列に割り当てられたメモリを所有しているため、これを破棄する必要はありません。

static config_t cfg;
config_t *cf;

// config_destroy(cf); <-- don't!

プログラムの実行ごとに 1 回だけ構成を読み取る場合、これは問題にはなりません。

于 2015-08-27T07:16:37.783 に答える