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

char *pBuf = new char[1000025];

is. (See code below) The following is code from the ::OnRecive function in my program.

{
	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);
	}

}

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?

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.

Well perni's solution should work. But why cant you use a normal array like

char pBuf = char[1000025]

? It will cleanup itself as it goes out of scope. Saves you a lot of trouble.

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?)).

Well perni's solution should work. But why cant you use a normal array like

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

char pBuf = char[1000025];

and

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:

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...

Yeah my bad. I meant

char pBuf [1000025];

I shouldn't have edited your post on the fly. Sorry about that. It should clear itself when going outof scope.

Yeah my bad. I meant

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

Well I dont think you would get an error if you pass it as

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.

m_sConnectSocket.Receive( (void*)pBuf , 1000025, nFlags)

Still get an exception error, even if I do it both ways.

But what does the exception say ? (In (win32) c you hardly get exceptions unless its fatal, such as dereferensing a null pointer, or running out of memory)...

Socket Notification Sink: blah.exe - Application Error

The exception unknown software exception (0xc00000fd) occured in the application at location 0x00406257.

Click on OK to terminate the program
Click on CANCEL to debug the program

Well thats windows for you... unkown software exception... sugestion: debug program to try to get a full stack trace of where the exception occurs.

0xc00000fd is a StackOverflow Exception. Maybe your system can't allocate 1000025 characters from the stack. If that is the case I dont see any way other than carry out your original method ( using the new operator )of allocating it from the store.

Ok, so I shrank pBuf, and it worked. It's not leaking either(so far :P ) explain agian though, what's the differance? I've always had an issue understanding pointers. (call me an idiot) and why won't the pointer clear after it's been used?

EDIT:
(what is the largest size a char can go? and if I need a larger char, how do I do it?)

Oh and it messed w/ other parts of my program.. hehe but no worries I iron out those wrinkles

Where do you find such exception information? It should be written down somewhere on msdn like most error codes I guess (link please)?
(I've already explained the difference, if you do not want to read my post then so be it.)
*Edit) Stupid me, always google before you ask a question: http://www.iseran.com/Win32/FAQ/except.html

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.