PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

what is the best way to trap 'NEW' failling to allocate memo

Using VS2005, I am having issues with NEW failing where the same code works on eVC3. I'm not sure about the heap settings however that does not seem to be the whole issue.

So what's a guaranteed way to trap NEW failures (NEW may not be returning null when it fails)???
If "new" fails, it will return 0 (NULL). If it succeeds, it will return a valid pointer.

So a guaranteed way of trapping "new" failures is to check the returned pointer for 0 (NULL).
if NEW was returning a NULL I could trap it easily however VS2005 seems to do something else. I am getting code stepping over my test for NULL and an invalid pointer. In addition I don't know if Hekkus uses NEW or not but it is now not failing when it fails to load a WAV resource. The pointer to the data is invalid but Hekkus thinks it is and plays it causing a crash. I should point out that it's not Hekkus or my code which remain unchanged but instead it's the compiler & linker which I think have changed in some way I'm not yet understanding. All my game works except WAVs.
You might want to move this to the Developer's forum.

So if I understand you correctly, you claim that operator new returns a non-NULL address, but then when this memory is supposedly accessed by a 3rd Party library it crashes?

Have you tried to access the memory yourself immediatly after the the allocation to see if the problem still exists?

If you're using new to allocate a buffer, have you ensured that the buffer is the correct size? If it's too small, the returned address would be valid, but the library may think that it's larger and caused an overrun.

Have you looked at the crash in the debugger to determine if it's due to reading/writing to this buffer that you've allocated?

Can you post the line of code that makes the call to new, along with the assembly code generated by the compiler for the call?
How did this get in the general forum??? Will see if I can move.
I can't post the code that fails cause there's no debug version of Hekkus. All I know is that I can force the error or make it go away by changing where it does the loading. As this code HAS NOT CHANGED since eVC3 it's a setup issue or linker or memory management change or whatever but very hard to do much until I know I can trap the errors happening.

As I can't debug Hekkus I am moving the problem into my code by loading Hekkus WAVs earlier. I can then see that NEW does not act correctly. I will have time on Friday to do a sample to show what happens I hope.

No option to move to another forum, Do I need a moderator to do this??? :wink:
Movededededed.
Hi Ian, I had similiar problem with Hekkus, loading wav's that crash the game, but later it appeared that it's not because of Hekkus or something else returns me the wrong pointer, it was because of my small STL error, that changed the memory here and there, it was OK for Surface since just some pixels was affected(changed theirs color), but it wasn't OK for Hekkus playing wav.
BTW it crashed my code ONLY in ARM(2000SDK) build.

So, maybe you should try the Hekkus sample, not your own code? If it is not crashing, then maybe your code...
first, operator new is not supposed to return NULL. have a look at this pseudo implementation of new (taken from Scott Meyers' Effective C++).

Code:

void * operator new(size_t size) // your operator new might
{ // take additional params

if (size == 0) { // handle 0-byte requests
size = 1; // by treating them as
} // 1-byte requests

while (1) {
attempt to allocate size bytes;

if (the allocation was successful)
return (a pointer to the memory);

// allocation was unsuccessful; find out what the
// current error-handling function is (see Item 7)
new_handler globalHandler = set_new_handler(0);
set_new_handler(globalHandler);

if (globalHandler) (*globalHandler)();
elsethrow std::bad_alloc();
}
}


i was sure that there was a function such as isValidPtr but i can't find it right now.

ofcourse you can take the problem into your own hands and write your own new and delete operators using malloc/free or your own heap implementation internally (don't forget to overload the array operators too...)

bye,
Daniel
Or maybe look at STLPort's allocate() to make it faster.
drgoldie wrote:first, operator new is not supposed to return NULL.


Oh but it will. Especially on Pocket PCs where C++ exceptions aren't supported.
Kzinti is correct. From MSDN:

If there is insufficient memory for the allocation request, by default operator new returns NULL. You can change this default behavior by writing a custom exception-handling routine and calling the _set_new_handler run-time library function with your function name as its argument. For more details on this recovery scheme, see The operator new Function.

I'm pretty certain that C++ EH is now supported on WM devices and has been for a while. More info here. I never use the stuff though.
InexorableTash · 07 Dec 2005, 05:00 UTC
Note that older Microsoft C++ compilers used a "non-throwing new" which returned NULL. Newer ones default to a "throwing new", but you can change this with a compiler flag.

This sounds like it could be the source of the problem; code written for devices compiled with eVC++ 3/4 would probably have returned NULL. In VS2005 the compiler front end is the same between desktop and devices, so it may be a throwing new by default.

This has a partial explanation: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclang/html/_pluslang_the_new_and_delete_operators.asp
Many thanks guys, I have a day's leave on Friday to play with this. The Hekkus code was compiled with eVC3 I think so I have no control of that but I can set up the exception handler for my NEW requirements.

The thing I still don't understand here is that the WAV files Hekkus loads are there and working from the main loop but not working in the actual functions that call them so I have 2 issues not just the NEW thing.
As I remeber Hekkus license, if you provided donation you can get the source code. Anyway I'm sure Thomas Maniero would be glad to give you the source code of Hekkus to make things work with VS2005.

http://www.shlzero.com/modules.php?name ... page&pid=2

Hope it helps,

P.S. Thank you for pioneering through VS2005 ;)
I did make a good 'donation' but don't remember being offered source code. Will check
Conan · 09 Dec 2005, 14:33 UTC — Found my problem at last (My code sort of)
My final problem was eventually traced to my using the wrong Hekkus include file. Wavs played just after you loaded them but elsewhere in the game they failed and generated errors which made me think there was a memory allocation issue.

I tried the event handler and that's a very useful addition to my still feeble C++ knowledge. I will always use this when creating my stuff with NEW.

Now if only Microsoft would actually sell me VS2005 full version (still got the beta) I will be happy