Finally nailed the bug in my
The code was stuck in an endless loop because it did not return an empty
dirent when it reached the end of the directory – instead returning the list of files in the directory over and over again.
To fix this I made sure that any following scan of the directory began where the previous one left off. As this would return an empty
dirent, the problem was solved.