Since the files are FILE * , we could do something like this:
In the header file that gets everywhere:
#define fopen(x, y) debug_fopen(x, y, __FILE__, __LINE__) #define fclose(x) debug_fclose(x)
in "debugfile.cpp" (obviously DO NOT use the above #define )
struct FileInfo { FileInfo(const char *nm, const char fl, int ln) : name(nm), file(fl), line(ln) {} std::string name; const char *file; int line; }; std::map<FILE*, FileInfo> filemap; FILE *debug_fopen(const char *fname, const char *mode, const char *file, int line) { FILE *f = fopen(fname, mode); if (f) { FileInfo inf(fname, file, line); filemap[f] = inf; } } int debug_fclose(FILE *f) { int res = fclose(f); filemap.erase(f); return res; }
(I did not compile this code, and it meant showing the concept, it might have small errors, but I think the concept will be executed - while your code, and not some third-party library, is leaking)
source share