C++ fopen memory leak

delphi package - automated exception handling
Post Reply
bwilt
Posts: 14
Joined: Tue Oct 22, 2013 10:14 pm

C++ fopen memory leak

Post by bwilt »

I've just grabbed the MaxEcept 4.0.20 for testing for a potential project using C++ language and RadStudio 10.2.2.

I have it running but have some library code that uses fopen quite a bit and get memory leaks from those functions.

Example code :-

Code: Select all

	char* filename = "w:\\test.txt";
	char* mode = "r";
	FILE* fd = fopen(filename, mode);
	if (fd != NULL) {
		fclose(fd);
	}
Indicates a memory leak at fclose(fd) ?

Also fstat line causes memory leak when that is added :-

Code: Select all

	char* filename = "w:\\test.txt";
	char* mode = "r";
	FILE* fd = fopen(filename, mode);
	if (fd != NULL) {
		struct stat statbuf;
		int handle = fileno(fd);
		fstat(handle, &statbuf);
		fclose(fd);
	}
Is there a way to figure out why or I guess potentially skipping leaks from fopen and fstat ?
madshi
Site Admin
Posts: 10754
Joined: Sun Mar 21, 2004 5:25 pm

Re: C++ fopen memory leak

Post by madshi »

Sorry for the late reply.

I've just reproduced the issue (no problem) and done some digging. The first thing I checked was the leak report about the critical section reported in "__lock_stream". So I searched for that and found the function "_lock_stream" in the file "streams.c". After carefully analyzing the code, I came to the conclusion that it's a true leak in the C++ RTL. At this point I stopped my research. I don't really have the resources to go hunt for BCB RTL bugs, it's simply not my job.

To be honest, I sometimes regret having added leak reporting capability, because a pretty large percentage of my support work now consists of analyzing other people's code to find out why madExcept reports a leak there. In most cases it's a true leak, but still it's my time that is being lost on double checking if madExcept has a bug or not... :(

If in doubt, please assume that madExcept's leak report is correct.
bwilt
Posts: 14
Joined: Tue Oct 22, 2013 10:14 pm

Re: C++ fopen memory leak

Post by bwilt »

Hi Madshi, its taken a while to find a moment to respond as well.

Thankyou for looking into this, I had posted this query as with CodeGaurd which is the C++ leak detector for the old compiler (and is super old) was not reporting anything in terms of a leak for the same code.

So after your response I investigate further and found that I had to directly link all of the source code for the fopen() into the project before that code would start report any leaking.

So I did manage to confirm your leak reports and it is correct, I managed to plug the leak but it was still reporting a couple of lines which I should try and figure report to you as I wasn't quite sure how to resolve those.

Please don't regret your efforts in creating the leak report and also your testing of peoples queries, your efforts in this regard is one of the reasons you are recommended by Delphi users over other products and is why we actually tried your product to start with.
madshi
Site Admin
Posts: 10754
Joined: Sun Mar 21, 2004 5:25 pm

Re: C++ fopen memory leak

Post by madshi »

Thank you for the feedback! :D
Post Reply