我正在用 c 编写一个严重依赖正则表达式的程序,而我执行它们的机制在 99% 的时间内都有效,但是它每隔一段时间就会使程序崩溃,我很难理解为什么会这样。
New_Sifter() 接受其正则表达式的字符串表示和一个处理函数,该函数接受一个或多个数组并返回单个字符串。
Sifter* New_Sifter(const char* exp, const char*(*func)(const char**, size_t)){
Sifter *sifter = malloc(sizeof(Sifter*));
sifter->strRegEx = exp;
if(regcomp(&(sifter->regEx), exp, REG_EXTENDED)){
printf("Could not compile regular expression\n");
exit(1);
}
sifter->Sift = &Base_;
sifter->Custom = func;
sifter->nGroups = sifter->regEx.re_nsub + 1;
sifter->captures = malloc(sifter->nGroups * sizeof(regmatch_t));
Register_Disposable(sifter->captures); //stores pointer in registry to be freed later
Register_Disposable(sifter); //stores pointer in registry to be freed later
return sifter;
}
const char* Base_(Sifter* self, const char* source){
if(regexec(&(self->regEx), source, self->nGroups, self->captures,
REG_EXTENDED) != 0){
printf("about to return null\n");
return NULL;
}
return self->Custom(
//Sift_() returns an array of the strings captured in the regexec
Sift_(source, self->captures, self->nGroups), self->nGroups);
}
当我运行它(并使用 gdb 调试一些)时,有时会出现错误,如下所示:
程序收到信号 EXC_BAD_ACCESS,无法访问内存。原因:KERN_INVALID_ADDRESS 地址:0x0000000000000008 0x00007fff90d72b9f in tre_reset_last_matched_branches () (gdb) bt
- 0x00007fff90d72b9f 在 tre_reset_last_matched_branches ()
- tre_fill_pmatch() 中的 0x00007fff90d72a58
- tre_match()中的0x00007fff90d72e56
- 0x00007fff90d72d35 在 regnexec ()
- 0x00000001000030cf in Base_ (self=0x1001000e0, source=0x1000033ee "add 11111, 22222, 33333")