944,120 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 3807
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Dec 13th, 2005
0

Memory Leak?

Expand Post »
So I'm writing a program (MFC) that connects to a server w/ windows sockets (YUCK I know). I left it running as part of an endurance test the other day, and my PC ran out of virtual memory. Odd eh? So I ran it again using task manager and noted that it's taking approx. 5,000k or memory everytime it recives and doesn't release the memory until the program is killed. I assume recieves because the amount of memory it takes up is related to how big
  1. char *pBuf = new char[1000025];
is. (See code below) The following is code from the ::OnRecive function in my program.
  1. {
  2. char *pBuf = new char[1000025];// yeah yeah i know it's huge!!
  3. int iBufSize = 10000024;
  4. int iRcvd;
  5. CString strRecvd;
  6. // Receive the message
  7. iRcvd = m_sConnectSocket.Receive(pBuf, iBufSize);
  8. // Did we receive anything?
  9. if (iRcvd == SOCKET_ERROR)
  10. {
  11. }
  12. else
  13. {
  14. // Truncate the end of the message
  15. pBuf[iRcvd] = NULL;
  16. // Copy the message to a CString
  17. strRecvd = pBuf;
  18. m_pBuf = pBuf;
  19. m_strOutput = strRecvd;
  20. // Sync the variables with the controls
  21. // UpdateData(FALSE);
  22. }
  23.  
  24. }
So!!! Here in lies the question. Why does it keep taking memory and not releasing it (until the program is killed) and how to I get it to release the memory?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
atrusmre is offline Offline
64 posts
since Mar 2005
Dec 13th, 2005
0

Re: Memory Leak?

Because you dont release the memory ? (There is no delete matching the new)... (However most implementations of delete would not actually return the memory to the system anyway, it would just put it in the local free lists)...
*Edit)
If you wanted to allocate lots of memory and be sure to return it to the system you could probably use one winapi function to get you a (several?) fresh page(s), use it and then explicitly return it to the system.
Reputation Points: 29
Solved Threads: 4
Junior Poster in Training
perniciosus is offline Offline
78 posts
since Nov 2005
Dec 13th, 2005
0

Re: Memory Leak?

Well perni's solution should work. But why cant you use a normal array like
  1. char pBuf = char[1000025]
? It will cleanup itself as it goes out of scope. Saves you a lot of trouble.
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Dec 13th, 2005
0

Re: Memory Leak?

I suppose you mean char pBuf[LARGE_INTEGER_VALUE], the usual problem with that is you easily get stack overflow due to restrictive stack memory allocation, even if I doubt you need such a large buffer... then again I'm not sure where that limit goes, and it tends to vary between compilers/operative systems (in unix/linux you can usually change it by using limit (or is it ulimit?)).
Reputation Points: 29
Solved Threads: 4
Junior Poster in Training
perniciosus is offline Offline
78 posts
since Nov 2005
Dec 13th, 2005
0

Re: Memory Leak?

Quote originally posted by WolfPack ...
Well perni's solution should work. But why cant you use a normal array like
  1. char pBuf = char[1000025]
? It will cleanup itself as it goes out of scope. Saves you a lot of trouble.
What's the difference between
  1. char pBuf = char[1000025];
and
  1. char *pBuf = new char[1000025];
when the second one goes out of scope won't it cleanup itself also?

Side note. I tried changing it to what you suggested and I get a bunch of errors.

As clearification<SP>. When the program receives data, it uses say 5,000k. Next time it recieves data it uses another 5,000k. Now my total memory used is 10,000k. This keeps going until I end the program. As you can see this could cause a problem. Eventually I run out of ram. I'm just wondering why? and how to fix it. :cheesy:
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
atrusmre is offline Offline
64 posts
since Mar 2005
Dec 13th, 2005
0

Re: Memory Leak?

As I said char pBuf = char[1000025]; is syntax error and does not mean anything...
char pBuf[10000025]; mean declare as local variable and store on stack
char *pBuf = new char[10000025]; mean declare a (local variable) pointer to allocated memory (that must be explicitly deleted)... you do not delete this memory...
all execution paths must include a delete [] pBuf; or hand it over to some other location that must take responsibility for destruction else you will have a memory leak...
Reputation Points: 29
Solved Threads: 4
Junior Poster in Training
perniciosus is offline Offline
78 posts
since Nov 2005
Dec 13th, 2005
0

Re: Memory Leak?

Yeah my bad. I meant
  1. char pBuf [1000025];
I shouldn't have edited your post on the fly. Sorry about that. It should clear itself when going outof scope.
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Dec 13th, 2005
0

Re: Memory Leak?

Quote originally posted by WolfPack ...
Yeah my bad. I meant
  1. char pBuf [1000025];
I shouldn't have edited your post on the fly. Sorry about that. It should clear itself when going outof scope.
The only problem with that though is that m_sConnectSocket.Receive only takes ( void *lpBuf, int nBufLen, int nFlags = 0) as arguments. if I do what you suggested I get an exception error
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
atrusmre is offline Offline
64 posts
since Mar 2005
Dec 13th, 2005
0

Re: Memory Leak?

Well I dont think you would get an error if you pass it as
  1. m_sConnectSocket.Receive( pBuf , 1000025, nFlags)
. A void* should take any kind of pointer.
If it does still give an exception, try doing a cast.
  1. m_sConnectSocket.Receive( (void*)pBuf , 1000025, nFlags)
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Dec 14th, 2005
0

Re: Memory Leak?

Still get an exception error, even if I do it both ways.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
atrusmre is offline Offline
64 posts
since Mar 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Trying to understand this code???
Next Thread in C Forum Timeline: initializing a hash_set ?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC