Operating System - HP-UX
1819805 Members
3088 Online
109607 Solutions
New Discussion юеВ

memory leak in fopen() and pthread_create?

 
Dave Harrison_2
New Member

memory leak in fopen() and pthread_create?

Purify's memory in use at exit report is showing that fopen() and various pthread functions appear to be leaking memory. I'm running hpux 11.0. It looks like at least 220 bytes per thread created leaks. Can anyone confirm this? I'm using the -mt compile flag with aCC A.03.37.
8 REPLIES 8
Adam J Markiewicz
Trusted Contributor

Re: memory leak in fopen() and pthread_create?

Hi

I think you don't have to worry about this.
I guess these founctions just initialize buffers needed for new file handle, or thread slot. They are allocated on demand, that is at the first time they are used and should be deallocated at the library cleanup, that is just at the end of process.

Just as a test you could try to close and reopen the file (confirm that you will get the same handle each time). If the handle is the same, but it allocates resources each time when it is returned, then you have something to worry.
I bet the allocation will be only at the first time.

Good luck
Adam
I do everything perfectly, except from my mistakes
Dave Harrison_2
New Member

Re: memory leak in fopen() and pthread_create?

Thank you for responding so quickly. I appreciate it.

Purify doesn't show any memory fragments asoociated with fopen unless I open six files simultaneously in a process that creates one detached thread. I open the files in the thread and not in the main thread. Regarding the test you suggest I run, I'm not sure what you mean?
Adam J Markiewicz
Trusted Contributor

Re: memory leak in fopen() and pthread_create?

Hi
What do you mean by
'open six files _simultaneously_'?
You mean one by one, but not in the main thread?

The test I was thinking of is reopening the same filehandle again. If it was already initialized, on the first open, it shouldn't be initialized again. Something like below:

FILE *first, *last;

first = fopen( /*...*/); // the buffers are initialized;
fclose( first ); // we have to free it to allow to be reused again.
for( int i = 10 ; i ; --i ) {
last = fopen( /*...*/);
if( last == first ) {
// the same handle - should be initialized already, so no new allocations expected
} else {
// different handle - everything can happen
}
fclose( last );
}

Good luck
Adam
I do everything perfectly, except from my mistakes
Dave Harrison_2
New Member

Re: memory leak in fopen() and pthread_create?

Hi Adam,

Re the simultaneously open files, follows is an example of what a created thread does (not in the main thread):
f1 = fopen();
f2 = fopen();
f3 = fopen();
f4 = fopen();
f5 = fopen();
f6 = fopen();
fclose(f1);
fclose(f2);
fclose(f3);
fclose(f4);
fclose(f5);
fclose(f6);

Re the test you suggested, 'last == first' happens everytime. Would it help if I attached the output from Purify's 'memory in use at exit' report?

Many thanks for you help!

Regards, Dave
Adam J Markiewicz
Trusted Contributor

Re: memory leak in fopen() and pthread_create?

Hi

I'm not surprized that last == first.
But the thing this was about to test is - if the memory usage grows, as you do more fopen() fclose() calls? In other words - if you change the value initializing index 'i' in loop - do you have different results?

If yes - this can mean leakage.
If not - I wouldn't worry any more.

Good luck
Adam
I do everything perfectly, except from my mistakes
Dave Harrison_2
New Member

Re: memory leak in fopen() and pthread_create?

Hi Adam,

Yes, heap use grows. I've confirmed this via Purify and by adding mallinfo() calls.

I'm wondering if these leaks are fixed via patches or if I've missed a compiler/linker flag?

Thanks, Dave
Adam J Markiewicz
Trusted Contributor

Re: memory leak in fopen() and pthread_create?

Hi

Well, so you have the bad news afterall.
Unluckilly I don't know how to help next.
Maybe there is some patch for it.

Hey! People with HP logo! Don't just sit there - say something.

Good luck
Adam
I do everything perfectly, except from my mistakes
Dave Harrison_2
New Member

Re: memory leak in fopen() and pthread_create?

Thank you for your efforts Adam. I appreciate it! Regards, Dave