View Single Post
Old 09-16-2005, 08:18 AM   #1 (permalink)
TheSheep
Registered User
 
TheSheep's Avatar
 
Join Date: Mar 2005
Location: UK
Posts: 11
TheSheep is on a distinguished road
Send a message via ICQ to TheSheep Send a message via AIM to TheSheep Send a message via MSN to TheSheep Send a message via Yahoo to TheSheep
Question new fails, no exception, no handler

Code:
Code:
int MyNewHandler( size_t size )
{
puts("[MyNewHandler] Allocation error!");

return 0; //abort
}

void TryMemAlloc()
{
unsigned char *c;

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

_set_new_handler(MyNewHandler);

try
	{
	c=new unsigned char;
	}
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");
}
Output:
Code:
[TryMemAlloc] Started......
[TryMemAlloc] char not allocated!
[TryMemAlloc] Done
As you can see, new fails but gives no indication that it's out of memory by throwing an exception (which AFAIK it shouldn't do when I set up my own handler, OK) or calling MyNewHandler. And I don't have any reason to think it would run out of memory either, especially when allocating 1 byte!!
Obviously I'm not doing all this just to allocate a byte - the same problem occurs when trying to dynamically allocate a class I've made.

Compiled using MS VC++ 5.0, non-MFC Win32 DLL, multithreaded (2 threads).


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.
TheSheep is offline   Reply With Quote