| | |
Memory Leak?
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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
is. (See code below) The following is code from the ::OnRecive function in my program.
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?
C Syntax (Toggle Plain Text)
char *pBuf = new char[1000025];
C Syntax (Toggle Plain Text)
{ char *pBuf = new char[1000025];// yeah yeah i know it's huge!! int iBufSize = 10000024; int iRcvd; CString strRecvd; // Receive the message iRcvd = m_sConnectSocket.Receive(pBuf, iBufSize); // Did we receive anything? if (iRcvd == SOCKET_ERROR) { } else { // Truncate the end of the message pBuf[iRcvd] = NULL; // Copy the message to a CString strRecvd = pBuf; m_pBuf = pBuf; m_strOutput = strRecvd; // Sync the variables with the controls // UpdateData(FALSE); } }
Thanx,
Atrus
Atrus
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.
*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
Only two things are infinite, the universe and human stupidity, and I'm not sure about the former. Albert Einstein
Well perni's solution should work. But why cant you use a normal array like ? It will cleanup itself as it goes out of scope. Saves you a lot of trouble.
C Syntax (Toggle Plain Text)
char pBuf = char[1000025]
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
Only two things are infinite, the universe and human stupidity, and I'm not sure about the former. Albert Einstein
•
•
•
•
Originally Posted by WolfPack
Well perni's solution should work. But why cant you use a normal array like? It will cleanup itself as it goes out of scope. Saves you a lot of trouble.C Syntax (Toggle Plain Text)
char pBuf = char[1000025]
C Syntax (Toggle Plain Text)
char pBuf = char[1000025];
C Syntax (Toggle Plain Text)
char *pBuf = new char[1000025];
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
Atrus
As I said
all execution paths must include a
char pBuf = char[1000025]; is syntax error and does not mean anything...char pBuf[10000025]; mean declare as local variable and store on stackchar *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
Only two things are infinite, the universe and human stupidity, and I'm not sure about the former. Albert Einstein
Yeah my bad. I meant I shouldn't have edited your post on the fly. Sorry about that. It should clear itself when going outof scope.
C Syntax (Toggle Plain Text)
char pBuf [1000025];
•
•
•
•
Originally Posted by WolfPack
Yeah my bad. I meantI shouldn't have edited your post on the fly. Sorry about that. It should clear itself when going outof scope.C Syntax (Toggle Plain Text)
char pBuf [1000025];
Thanx,
Atrus
Atrus
Well I dont think you would get an error if you pass it as . A void* should take any kind of pointer.
If it does still give an exception, try doing a cast.
C Syntax (Toggle Plain Text)
m_sConnectSocket.Receive( pBuf , 1000025, nFlags)
If it does still give an exception, try doing a cast.
C Syntax (Toggle Plain Text)
m_sConnectSocket.Receive( (void*)pBuf , 1000025, nFlags)
![]() |
Similar Threads
- Can't prevent memory leak (C++)
- can't find my memory leak (C++)
- “Windows Virtual Memory Size Too Low� in XP (Windows NT / 2000 / XP)
- Constructor and Convertion operator are the same,how to avoid memory leak? (C++)
Other Threads in the C Forum
- Previous Thread: Trying to understand this code???
- Next Thread: initializing a hash_set ?
Views: 3324 | Replies: 15
| Thread Tools | Search this Thread |
Tag cloud for C
* adobe api append array arrays bash binarysearch char character cm copyanyfile copypdffile createcopyoffile createprocess() csyntax directory drawing dynamic executable execv feet fgets file floatingpointvalidation fork frequency function getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling gtkwinlinux highest homework i/o ide infiniteloop initialization interest intmain() iso keyboard kilometer lazy license linked linkedlist linux list lowest matrix meter microsoft mqqueue multi mysql oddnumber odf open openwebfoundation overwrite pause pdf pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scheduling segmentationfault send single socketprogramming spoonfeeding stack standard strchr string student suggestions system test testautomation unix urboc user whythiscodecausesegmentationfault win32 win32api windows.h






