phobos icon indicating copy to clipboard operation
phobos copied to clipboard

Regex leaks memory when used in multithreaded environment

Open dlangBugzillaToGithub opened this issue 3 years ago • 2 comments

qigezx+dc40d6nao940k reported this on 2022-10-07T16:07:24Z

Transfered from https://issues.dlang.org/show_bug.cgi?id=23395

Description

Each thread where regular expressions are used leaks memory exactly once, I think that only the first created regex leaks.
This is true for (CTR) compile time and runtime regex. CTR leak much more memory. The leak grows or shrinks depending on the size of the regex.
Compile the following example with "ldc2 -fsan=address -g" to see for yourself. It also works with gdc and dmd, but at least for dmd you need to use valgrind instead, which is more ambigious and less informative in it's reporting for this bug. 
```
import std;

void threadFun(){
    auto rgx = regex(r"(?P<num>\d)");
    auto res = matchFirst(words, rgx);
    res["num"].writeln;
}

void main(){
    foreach(i; 0..10){
        task!(threadFun).executeInNewThread();
    }
}
```

dlangBugzillaToGithub avatar Oct 07 '22 16:10 dlangBugzillaToGithub

qigezx+dc40d6nao940k commented on 2022-11-10T20:27:05Z

This is especially important when using unit_threaded as this is also when you want to enable fsan

dlangBugzillaToGithub avatar Nov 10 '22 20:11 dlangBugzillaToGithub

Hi! The alleged memory leak traces back to this function call [1]. Here, some heap memory is allocated by the factory and is assigned to the static variable cache. As far as I can tell, for previous allocations, the memory is correctly freed by decreasing the refcount. Thus the only "memory leak" that is detected is the heap memory bound to the static variable cache, which we cannot deallocate until the end of the program, since it is, well, static and has to be valid at any time the matchOnce function is called.

That being said, please let me know if there’s anything else I can assist with regarding this issue.

Albert24GG avatar Mar 14 '25 22:03 Albert24GG