Bonjour,

J'ai un SIGTRAP lors d'un appel de la fonction free mais je ne comprends pas pourquoi.
L'erreur se produit dans la fonction csv_deallocate(p) -> destroy_1D_c(p->raw); -> free(p)
Normalement j'ai ajouté les fonctions nécessaires.

Merci d'avance pour vos lumières !

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
int main(int argc, char *argv[], char* env[])
{
    logger_on();

    t_csv *p = csv_allocate("D:\\fichier.csv");
    csv_deallocate(p);

    logger_off();
    logger_read();
    return 0;
}

void csv_deallocate(t_csv * p)
{
    if(p)
    {
        if (p->raw)
            destroy_1D_c(p->raw);

        if (p->array)
            destroy_3D_c(p->array);

        free(p);
    }
}

void destroy_1D_c( t_1Dc *s)
{
    if (s)
    {
        if (s->da)
            deallocate_1D_c(s->da);

        free(s);
    }
    s=NULL;
}

void deallocate_1D_c( char *p )
{
    if (p)
    {
        free(p); /// SIGTRAP ICI ///
        p=NULL;
    }
    else fprintf(logger,"deallocate.h::deallocate_1D_c -> %s\n",strerror(errno));
}

t_csv *csv_allocate_empty()
{
    t_csv *p = NULL;
    if ( !(p = (t_csv*)malloc(sizeof(t_csv))) ) /// T1
        fprintf(logger, "csv.h::csv_allocate_empty.T1 -> The function has returned a NULL pointer\n");

    return p;
}

t_csv *csv_allocate( const char *path )
{
    t_csv *p = NULL;
    if ( (p = csv_allocate_empty()) ) ///T0
     {
        if ( path ) ///T1
        {
            if ( (p->path = str_copy(path)) ) ///T2
            {
                if ( (p->raw = create_empty_1D_c()) ) ///T3
                {
                    if ( (p->raw->da = file_to_string(path)) ) ///T4
                    {
                        p->raw->x = str_length(p->raw->da);
                        p->line   = csv_get_line( p );
                        p->row    = csv_get_row ( p );
                        if ( p->line != 0 && p->row != 0 ) ///T5
                        {
                            if ( (p->array = create_3D_c( p->line, p->row, 1000 )) ) ///T6
                                p->array = csv_format(p);

                            else
                            {
                                destroy_1D_c(p->raw);
                                deallocate_1D_c(p->path);
                                free(p);
                                fprintf(logger, "csv.h::csv_allocate.T6 -> Memory allocation of the t_3Dc structure's Array member has failed\n");
                            }
                        }
                        else
                        {
                            destroy_1D_c(p->raw);
                            deallocate_1D_c(p->path);
                            free(p);
                            fprintf(logger, "csv.h::csv_allocate.T5 -> Lines and/or rows quantity are equals to 0\n");
                        }
                    }
                    else
                    {
                        deallocate_1D_c(p->path);
                        free(p);
                        fprintf(logger, "csv.h::csv_allocate.T4 -> Memory allocation of the t_1Dc structure's Array member has failed\n");
                    }
                }
                else
                {
                    deallocate_1D_c(p->path);
                    free(p);
                    fprintf(logger, "csv.h::csv_allocate.T3 -> Memory allocation of the t_1Dc structure's Array member has failed\n");
                }
            }
            else fprintf(logger, "csv.h::csv_allocate.T2 -> The function str_copy has returned a NULL pointer\n");
        }
        else fprintf(logger, "csv.h::csv_allocate.T1 -> The CSV file path's pointer is NULL\n");
    }
    else fprintf(logger, "csv.h::csv_allocate.T0 -> p is NULL\n");
    return p;
}


t_3Dc *csv_format( t_csv *p )
{
    t_3Dc *p_out = NULL;
    if ( p ) ///T0
    {
        if ( p->raw->da ) ///T1
        {
            if ( (p_out = create_3D_c(p->line, p->row, 1000)) ) ///T2
            {
                t_uint64 i, i_tmp, line, row;
                i_tmp = 0;
                line  = 0;
                row   = 0;

                for( i=0 ; i < p->raw->x ; i++ )
                {
                    if ( p->raw->da[i] == ';' )
                        i_tmp = 0, row++;

                    else if ( (p->raw->da[i] == '\n') )
                        i_tmp = 0, line++, row = 0;

                    else
                    {
                        if ( line >= p->line || row >= p->row || i_tmp >= 1000 )
                            printf("\n Possible SIGSEV signal while translating data to the 3D array\n line = %I64u ; current is %I64u\n line = %I64u ; current is %I64u\n row = %I64u ; current is %I64u\n i_tmp = %I64u ; current is %I64u", line , p->line, row, p->row, i_tmp, (t_uint64)1000 );

                        else
                        {
                            if (p->raw->da[i] != '\r')
                                p_out->da[line][row][i_tmp] = p->raw->da[i], i_tmp++;
                        }
                    }
                }
            }
            else fprintf(logger, "csv.h::csv_format.T2 -> The memory allocation of the csv array's pointer is NULL\n");
        }
        else fprintf(logger, "csv.h::csv_format.T1 -> The raw csv file's pointer is NULL\n");
    }
    else fprintf(logger, "csv.h::csv_format.T0 -> The t_csv structure's pointer is NULL\n");
    return p_out;
}

t_uint64 csv_get_line( t_csv *p )
{
    t_uint64 line = 0;
    if( p ) ///T1
    {
        if ( p->raw->da ) ///T2
            line = str_count_occurrence( p->raw->da, '\n' );

        else fprintf(logger, "csv.h::csv_get_line.T2 -> The char's pointer is NULL\n");
    }
    else fprintf(logger, "csv.h::csv_get_line.T1 -> The t_csv structure's pointer is NULL\n");
    return line;
}

t_uint64 csv_get_row( t_csv *p )
{
    t_uint64 max = 0;
    t_uint64 row = 0;
    if( p )
    {
        if ( p->raw )
        {
            t_uint64 i;
            for ( i=0 ; i<p->raw->x ; i++ )
            {
                if ( p->raw->da[i] == '\n' )
                {
                    if (row>max)
                        max=row;

                    row = 0;
                }

                if ( p->raw->da[i] == ';' )
                    row+=1;
            }
        }
        else fprintf(logger, "csv.h::csv_get_row -> da t_2Dc structure's member is NULL\n");
    }
    else fprintf(logger, "csv.h::csv_get_row -> t_csv structure is NULL\n");
    return max+1;
}


}