"Ashkaan Singh" <23*5*5*
3@s*u*e*t*u*a*e*u*a*> wrote:
> When using the regcomp() function my code works here and there. To elaborate I have a separate helper function that gets called to check if one of the patterns match it and that function gets called for every file that is found in a directory.
>
> The issue is that sometimes the program will break at first few files after using the matching function or it will break in the middle where the matching function was working for the other files but not when it gets to there.
>
> The reason why it breaks is because it says "malloc: heap corruption detected free list is damaged". I make sure to use the regfree() function to clear space that was allocated to it.
Hello Ashkaan,
It's almost impossible to diagnose the problem from your description, alone.
Each process has a single heap, and that heap is shared by all (many) functions used in your program.
We're aware of malloc/realloc/.... that directly manage our own memory requirements, but other functions such as fopen/opendir/readir/regcomp/regexec/.... also use that single heap. Thus any error or invalid memory access that corrupts the heap will have have a, usually fatal, effect when you use other functions, and those errors often 'reveal' themselves long after they have been caused.
So it may not be the regex functions themselves that are raising the error. Do you know if/which exact regex function is raising the error?
Also, just a hunch, you say that you're calling regfree() - is that at the end of your program, or after every attempt to match? If the latter, are you calling regcomp(), regexec(), and regfree() for every attempt to match? If so, instead, your could just call regcomp() once (in the whole program) for each pattern, match with the saved compiled patterns, and never call regfree().