Memory Leak?

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2005
Posts: 64
Reputation: atrusmre is an unknown quantity at this point 
Solved Threads: 0
atrusmre's Avatar
atrusmre atrusmre is offline Offline
Junior Poster in Training

Memory Leak?

 
0
  #1
Dec 13th, 2005
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?
Thanx,
Atrus
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 78
Reputation: perniciosus is an unknown quantity at this point 
Solved Threads: 4
perniciosus's Avatar
perniciosus perniciosus is offline Offline
Junior Poster in Training

Re: Memory Leak?

 
0
  #2
Dec 13th, 2005
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.
/pern.*/i

Only two things are infinite, the universe and human stupidity, and I'm not sure about the former. Albert Einstein
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Memory Leak?

 
0
  #3
Dec 13th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 78
Reputation: perniciosus is an unknown quantity at this point 
Solved Threads: 4
perniciosus's Avatar
perniciosus perniciosus is offline Offline
Junior Poster in Training

Re: Memory Leak?

 
0
  #4
Dec 13th, 2005
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?)).
/pern.*/i

Only two things are infinite, the universe and human stupidity, and I'm not sure about the former. Albert Einstein
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 64
Reputation: atrusmre is an unknown quantity at this point 
Solved Threads: 0
atrusmre's Avatar
atrusmre atrusmre is offline Offline
Junior Poster in Training

Re: Memory Leak?

 
0
  #5
Dec 13th, 2005
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:
Thanx,
Atrus
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 78
Reputation: perniciosus is an unknown quantity at this point 
Solved Threads: 4
perniciosus's Avatar
perniciosus perniciosus is offline Offline
Junior Poster in Training

Re: Memory Leak?

 
0
  #6
Dec 13th, 2005
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...
/pern.*/i

Only two things are infinite, the universe and human stupidity, and I'm not sure about the former. Albert Einstein
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Memory Leak?

 
0
  #7
Dec 13th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 64
Reputation: atrusmre is an unknown quantity at this point 
Solved Threads: 0
atrusmre's Avatar
atrusmre atrusmre is offline Offline
Junior Poster in Training

Re: Memory Leak?

 
0
  #8
Dec 13th, 2005
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
Thanx,
Atrus
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Memory Leak?

 
0
  #9
Dec 13th, 2005
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)
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 64
Reputation: atrusmre is an unknown quantity at this point 
Solved Threads: 0
atrusmre's Avatar
atrusmre atrusmre is offline Offline
Junior Poster in Training

Re: Memory Leak?

 
0
  #10
Dec 14th, 2005
Still get an exception error, even if I do it both ways.
Thanx,
Atrus
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 3324 | Replies: 15
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC