2016-04-05 15 views
1

Cファイルプログラムでサイズ1の例外の読み込みが無効です。定義C valgrindからの無効な読み取り

==9072== Invalid read of size 1 
==9072== at 0x4C28FB2: __GI_strlen (mc_replace_strmem.c:404) 
==9072== by 0x401FA8: getJsonRecord (in /.autofs/ilab/ilab_users/csd92/Systems_Programming/Indexer/index) 
==9072== by 0x401CF3: jsonWrite (in /.autofs/ilab/ilab_users/csd92/Systems_Programming/Indexer/index) 
==9072== by 0x400BFC: main (index.c:42) 
==9072== Address 0x51d6e80 is 0 bytes inside a block of size 19 free'd 
==9072== at 0x4C27430: free (vg_replace_malloc.c:446) 
==9072== by 0x400F65: file_handler (index.c:110) 
==9072== by 0x400DBB: directory_handle (index.c:82) 
==9072== by 0x400DDC: directory_handle (index.c:84) 
==9072== by 0x400BC3: main (index.c:34) 

#define trailing_record() ("},\n\0") 
#define not_trailing_record() ("}\n\0") 
#define record_first() ("\t\t{\"") 
#define record_second() ("\" :") 

をこれは私のgetJsonRecordです:

char * getJsonRecord (char * token, char * frequency, int trailing) 
{ 

    if(token == 0 || frequency == 0) 
    { 
    return "Token or frequency == 0"; 
    } 

    char * entry = 0; 
    entry = calloc((strlen(token) + strlen(frequency) +30),1); 

    int tokensize= strlen (token); 
    int freqsize = strlen(frequency); 

    strcat(entry,record_first()); 
    strcat(entry,token); 
    strcat(entry,record_second()); 
    strcat(entry,frequency); 

    if(trailing == 0) 
    { 
    strcat(entry,not_trailing_record()); 
    } 

    else 
    { 
    strcat(entry,trailing_record()); 
    } 

    free(frequency); 
    return entry; 

} 

それが失敗した場合、これらはgetJsonRecordに渡されたばかりの値です:私はvalgrindのから、次の取得

token Index_Test/3:29:16, frequency 3 trailing 0 
token Index_Test/Temp/cable21.txt, frequency 1 trailing 1 

何か提案はありますか?

+2

あなたは 'のために渡されたものは何でものように見える場合token.c:110' –

+1

'getJsonRecord'は' frequency'を割り当てなかったので、おそらくそれも解放してはいけません。しかし、これは実際のバグではないかもしれません。実際のバグは、あなたが示していないコードにある可能性が高いです。 – zwol

+0

それに直面して、 'token'や' frequency'の1つが最近解放されたメモリを指しているので、あなたはまだ読んではいけませんそれ。どちらがいいのかは分かりません。 –

答えて

0

Ryan Hainingは正解でした。私のトークンは、ファイルが書き込まれる前に呼び出されることはないと思われる条件付きループで解放されていたことがあります。

固定される前に、条件付きループ:

if(filepath_index == (filepath_count)) 
{ 

    char ** temp = malloc(filepath_count *2 * sizeof(char *)); 

    int i = 0; 
    for (i = 0; i < filepath_count; i ++){ 
     temp[i] = malloc(strlen(filepaths[i])+1); 
     strcpy(temp[i],filepaths[i]); 
     free(filepaths[i]); 
    } 

    free(filepaths); 
    filepaths = temp; 
    filepath_count = filepath_count * 2; 
} 

固定された後に、条件付きループ: (filepath_index ==(filepath_count)) {

char ** temp = malloc(filepath_count *2 * sizeof(char *)); 
int i = 0; 

for (i = 0; i < filepath_count; i ++){ 
    temp[i] = filepaths[i]; 
} 

free(filepaths); 
filepaths = temp; 
filepath_count = filepath_count * 2; 
} 
関連する問題