View Single Post
Old 09-17-2005, 04:04 AM   #3 (permalink)
Locutus
Registered User
 
Join Date: Aug 2005
Posts: 20
Locutus is on a distinguished road
If I compile your code into a small program like this:
Code:
int MyNewHandler(size_t)
{
	puts("[MyNewHandler] Allocation error!");
	return 0;
}

int main()
{
	unsigned char *c;

	puts("[TryMemAlloc] Started......");

	_set_new_handler(MyNewHandler);

	try
	{
		c=new unsigned char[0x7fffffff];
	}
	catch(...)
	{
		puts("[TryMemAlloc] Exception thrown!");
	}
	if(c==0)
		puts("[TryMemAlloc] char not allocated!");
	else
		printf("[TryMemAlloc] char allocated at %p.\n",c);

	puts("[TryMemAlloc] Done");

	return EXIT_SUCCESS;
}
I get this as output (MSVC6):
Code:
[TryMemAlloc] Started......
[MyNewHandler] Allocation error!
[TryMemAlloc] char not allocated!
[TryMemAlloc] Done
Quote:
Originally Posted by TheSheep
Edit - I appreciate it's preferable to post full compilable code but it's not really possible. I have a large DLL with a few thousand lines and 18 different CPP modules. This is a late stage in the second thread, after much network I/O and HTTP downloading (using a separate, MFC DLL).
For that reason I'd appreciate any suggestions, no matter how general, and I'll try them out.
At first sight, I don't see anything obvious in your code that would prevent the handler from getting called. It is fairly rare for new to fail due to "memory exhaustion" these days, unless you've got for example a huge memory leak somewhere. It might be a good idea to have a look at how much memory your program allocates, for example by using the Windows task manager. (I assume you're running on Windows since you're using MSVC). The fact that the program is multithreaded doesn't make things easier either . In general, if you want us to be usefull, your best bet is probably to try and create the smallest possible program that fails in this way. In fact, it's quite possible that doing so leads to you finding the problem yourself .

Note that this isn't necessarily standard C++ either, since _set_new_handler is an MS specific "extension". (Arguably, doing *anything* semi-non-trivial with MSVC probably isn't standard )
Locutus is offline   Reply With Quote