3

I'm facing an unknown exception thrown after I call the new operator.

The new operator is called after a long processing time (10h) to store output results. This code is implemented in a DLL that is loaded from a C# application running in .Net 4.5.

I created a MWE with the code below put into a for loop, but it throws an std::bad_alloc as expected, while the real code throws unknown exception and falls to the catch(...) statement.

Note that I am using Visual Studio 2013.

This is the code:

try {

  char* output = new char[50000000];

} catch (const std::bad_alloc&) {
  // Doesn't happen
  printf("Bad Alloc\n");
} catch (const std::exception&) {
  // Doesn't happen
  printf("Standard Exception\n");
} catch (...) {
  // Code falls here
  printf("Unknown Exception\n");
}

If no one has a solution for this, can you at least please give ideas for debugging it? I've thought about using Windows API calls to check memory state before calling the new operator, but I need more ideas.

What are all types of exceptions MSVC can throw upon operator new and what can I do to retrieve information from those?

drescherjm
  • 10,365
  • 5
  • 44
  • 64
manatttta
  • 3,054
  • 4
  • 34
  • 72
  • 2
    Can you post the whole code? – kiner_shah Feb 01 '19 at 08:41
  • I don't understand what you are saying. If the use of `new` operator fails in the MWE, why would you expect for it to work after 10 hours of processing time ? – Norgannon Feb 01 '19 at 08:46
  • @Norgannon the code fails in MWE with std::bad_alloc, but in runtime it fails with unknown exception – manatttta Feb 01 '19 at 08:47
  • I'd probably try with a memory sanitiser and an external memory analyser. If they don't throw anything up then you may be looking at some UB (though i'd say this is unlikely if nothing else weird has happened over 10 hrs). – George Feb 01 '19 at 08:51
  • 1
    @kiner_shah I can't, sorry – manatttta Feb 01 '19 at 08:54
  • Do you have any idea of what kind of exception does it throw? maybe you can try using RTTI to print the actual exception type. Or just run it in a debugger and break at the catch. – bracco23 Feb 01 '19 at 09:00
  • When I tried this in VS 21013 I also got a bad_alloc. I am not sure how to reproduce. – drescherjm Feb 01 '19 at 09:00
  • @bracco23 how do you use RTTI for that? Could you perhaps provide an example? – manatttta Feb 01 '19 at 09:02
  • @manatttta you can use `typeid(exception).name() ` to print the name of the class. you can find more details [here](https://en.cppreference.com/w/cpp/language/typeid) – bracco23 Feb 01 '19 at 09:08
  • 1
    @bracco23 yes but the code is falling to the `catch(...)` case so how can I use `typeid` there? – manatttta Feb 01 '19 at 09:19
  • Do you have SEH enabled?? https://stackoverflow.com/questions/4414027/visual-c-unmanaged-code-use-eha-or-ehsc-for-c-exceptions also https://learn.microsoft.com/en-us/cpp/build/reference/eh-exception-handling-model?view=vs-2017 – drescherjm Feb 01 '19 at 09:19
  • It appears some CLR options enable SEH. Are you using clr? – drescherjm Feb 01 '19 at 09:25
  • @drescherjm not using CLR! – manatttta Feb 01 '19 at 11:16
  • @drescherjm actually this is implemented on a DLL that is loaded from a C# application. Does this mean it has CLR? Or that it runs in a managed environment? – manatttta Feb 01 '19 at 13:19
  • 1
    Is it a C++/CLI DLL (using normal reference from C#) or a plain C++ DLL (using P/Invoke from C#)? Also, is this 32-bit or 64-bit code? Anyway, a `catch(...)` reacting to `new` most likely means corrupted heap, i.e. the real problem is probably a buffer overflow somewhere else. – Sebastian Redl Feb 01 '19 at 13:29
  • @SebastianRedl yes I agree this is probably corrupted heap. This is 32bit, I think it uses PInvoke – manatttta Feb 01 '19 at 14:16

0 Answers0