Unhandled exception while assigning strings. C++ - c++

I'm using Visual Studio 2012 Express and I write in c++.
I have a rather weird problem with my code.
void CMap::loadMap(const std::string &MapName)
{
m_MapName = MapName; // This line is causing an error
loadMap();
}
While it compiles succesfully, it crashes on that line when I run it.
Unhandled exception at 0x003A10FC (msvcr110d.dll) in MyAdventure.exe: 0xC0000005: Access violation writing location 0xFEEEFEEE.
That's what it says. When i click Break, it directs me to that line in memcpy.asm
mov [edi],al ;U - write second byte to destination
CMap is a class which has
std::string m_MapName;
as it's member.
I've also tried to compile my code in Visual Studio 2010 Express and it works fine without any problems. I'd really want to use VS 2012 though, so I'd really like some help.
Thanks for any help.

Related

OpenCV DirectX 11 Interoperability

I need to read an image in OpenCV, send it to DirectX, do some processing, and then send the output image back to OpenCV. I am completely new to this, and was working with DirectX 11, therefore, I decided to refer this sample which demonstrates OpenCV and DirectX interoperability. There are options for both GPU and CPU modes, but right now, I plan to use only the CPU. The program builds without any errors, but returns the following runtime error everytime:
Exception thrown at 0x0000000000000000 in Direct3D Win32 my_project_name.exe: 0xC0000005: Access violation executing location 0x0000000000000000. occurred
I looked for solutions everywhere but couldn't find any. Since this is a sample, I guess many people might have used this.
Here are the lines where I'm getting an exception.
if (cv::ocl::haveOpenCL())
{
m_oclCtx = cv::directx::ocl::initializeContextFromD3D11Device(m_pD3D11Dev); //this is the line which throws the exception
}
m_oclDevName = cv::ocl::useOpenCL() ?
cv::ocl::Context::getDefault().device(0).name() :
"No OpenCL device";
On hovering the cursor above m_pD3D11Dev, this message is displayed by the intellisense:
m_pD3D11Dev | 0x000001f5a286c618 <No type information available in symbol file for d3d11.dll>
I am guessing that there is some error in my setup or some other linker error since this is a sample code provided by OpenCV(which i am assuming is obviously going to run). Any help or guidance would be appreciated.
I'm using DirectX 11 and OpenCV 3.4.4 to build(x64) this in Visual Studio 19. I also tried building(x64) it in Visual Studio 17, but the results were same.
Thanks in advance.

exception during string assignment from GDCM

I have a Qt application that I am building with newer versions of VC++ and GDCM. I had built the application previously with VC++ 2015, and old version of GDCM, and it compiled and ran just fine. Now I'm running into an exception in a string assignment.
gdcm::Reader r;
r.SetFileName(f.toStdString().c_str());
if (r.Read()) {
gdcm::StringFilter sf;
sf = gdcm::StringFilter();
sf.SetFile(r.GetFile());
std::string s;
/* get modality */
gdcm::Tag tag = gdcm::Tag(0x0008,0x0060);
s = sf.ToString(tag); // <-- runtime error here...
fileModality = QString(s.c_str());
/* get patientID */
s = sf.ToString(gdcm::Tag(0x0010,0x0020));
filePatientID = QString(s.c_str());
/* get protocol (seriesDesc) */
s = sf.ToString(gdcm::Tag(0x0008,0x103E));
fileProtocol = QString(s.c_str());
}
If I use the assign function, the error moves to the next line, in the conversion from a string to a c_str.
s.assign(sf.ToString(tag));
fileModality = QString(s.data());
I'm not sure what's going on, but it seems to be an issue with GDCM not properly returning a string object.
Edit: The error is a dialog box with the following
The inferior stopped because it triggered an exception.
Stopped in thread 0 by: Exception at 0x7ffaa7b8f621, code: 0xc0000005: read access violation at: 0xfffffffffff, flags=0x0 (first chance).
And the call stack, starting with my own function call
1 std::_Container_base12::_Swap_all xutility 239 0x7ff7764a7b7a
2 std::_String_alloc<std::_String_base_types<char,std::allocator<char>>>::_Swap_all xstring 2029 0x7ff7764a7af7
3 std::string::_Assign_rv_contents_with_alloc_always_equal xstring 2353 0x7ff7764a619d
4 std::string::_Assign_rv_contents xstring 2326 0x7ff7764a6132
5 std::string::operator= xstring 2308 0x7ff7764a37cd
6 MainWindow::GetFileType mainwindow.cpp 477 0x7ff77648784c
... <More>
I'm putting this here in case anyone encounters a similar problem.
I was able to figure out the problem. GDCM was compiled in Release... but my application was compiled in Debug. Somehow it linked just fine and mostly ran, but when it encountered a function call from the GDCM library, it crashed. Compiling everything in Release or Debug fixed it.

MATLAB API for C exception using matGetDir: matrix::serialize::EndOfFile at memory location

I try to read a mat file in a c++ Visual Studio 2017 project on Windows 7. I use the MATLAB API for C from Matlab R2017b.
I am able to run the matdgns.c example in Matlab using mex.
It's also possible to run the application in my c++ project and open the mat file using
pmat = matOpen(file, "r");
However, when I try to read the array list using matGetDir
dir = (const char **)matGetDir(pmat, &ndir);
I get an exception:
Exception thrown at 0x000007FEFCF4A06D in project_gTest.exe:
Microsoft C++ exception: matrix::serialize::EndOfFile at memory
location 0x000000000031F1D0.
How can I solve this issue? Am I missing another library?
Thank you!
I received the following answer from Matlab support which "solves" the "issue":
I assume that this Exception which you "get" is basically just a line of text shown in the "Output" Window of Microsoft Visual Studio 2017, right? And this is not actually crashing your application, correct?
If that is indeed the case, please note that MSVS 2017 does in fact show first chance exceptions in "Output". If such an exception is caught and handled, where in this particular case I do expect that the MATLAB libraries handled this, there is no actual issue though. It is perfectly valid to throw exceptions and then handle them. Only unhandled (second chance) exceptions would lead to real problems.
It should normally be perfectly safe to ignore that line which is shown in the Output and it is in fact expected that this Exception is thrown (and caught internally) when working with matGetDir.

Gtkmm c++ trouble with strings

I'm writing simple gui application in c++ with, which use gtkmm. I have trouble with gtk functions, which uses std::string type. For example: when i use function Glib::get_real_name(), which returns std::string application crashes with message "Unhandled exception at 0x00007FF97FB8AFF4 (msvcr110d.dll) in app.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.". The same situation happens with other gtk functions, which returns std::string. There is also problem with some functions, which arguments are std::string for example create image from file cannot open proper picture (paths are correct..). There's no problem with functions, which are using ustring, but many built-in gtk functions uses std::string and they dont have ustring alternatives. It's seems to be problem with character encoding, but I dont know how to solve that. I'm using ms visual studio 2012. Have anyone of had this problem before or you know how to solve that?
std::string d = "1m.jpg";
Glib::RefPtr<Gdk::Pixbuf> image = Gdk::Pixbuf::create_from_file(d);
-> here i got this error in terminal:
unhandled exception is signal handler:
domain: g-file-error-quark
code: 17
what : failed to open file '`p\u001f': Invalid Argument
it's seems that 1m.jpg is p\u001f for this app :D

Program crashes with 0xC000000D and no exceptions - how do I debug it?

I have a Visual C++ 9 Win32 application that uses a third-party library. When a function from that library is called with a certain set of parameters the program crashes with "exception code 0xC000000D".
I tried to attach Visual Studio debugger - no exceptions are thrown (neither C++ nor structured like access violations) and terminate() is not called either. Still the program just ends silently.
How does it happen that the program just ends abnormally but without stopping in the debugger? How can I localize the problem?
That's STATUS_INVALID_PARAMETER, use WinDbg to track down who threw it (i.e. attach WinDbg, sxe eh then g.
Other answers and comments to the question helped a lot. Here's what I did.
I notices that if I run the program under Visual Studio debugger it just ends silently, but if I run it without debugger it crashes with a message box (usual Windows message box saying that I lost my unsaved data and everyone is sooo sorry).
So I started the program wihtout debugger, let it crash and then - while the message box was still there - attached the debugger and hit "Break". Here's the call stack:
ntdll.dll!_KiFastSystemCallRet#0()
ntdll.dll!_ZwWaitForMultipleObjects#20() + 0xc bytes
kernel32.dll!_WaitForMultipleObjectsEx#20() - 0x48 bytes
kernel32.dll!_WaitForMultipleObjects#16() + 0x18 bytes
faultrep.dll!StartDWException() + 0x5df bytes
faultrep.dll!ReportFault() + 0x533 bytes
kernel32.dll!_UnhandledExceptionFilter#4() + 0x55c bytes
//SomeThirdPartyLibraryFunctionAddress
//SomeThirdPartyLibraryFunctionAddress
//SomeThirdPartyLibraryFunctionAddress
//SomeThirdPartyLibraryFunctionAddress
//OurCodeInvokingThirdPartyLibraryCode
so obviously that's some problem inside the trird-party library. According to MSDN, UnhandledExceptionFilter() is called in fatal situations and clearly the call is done because of some problem in the library code. So we'll try to work the problem out with the library vendor first.
If you don't have source and debugging information for your 3rd party library, you will not be able to step into it with the debugger. As I see it, your choices are;
Put together a simple test case illustrating the crash and send it onto the library developer
Wrap that library function in your own code that checks for illegal parameters and throw an exception / return an error code when they are passed by your own application
Rewrite the parts of the library that do not work or use an alternative
Very difficult to fix code that is provided as object only
Edit You might also be able to exit more gracefully using __try __finally around your main message loop, something like
int CMyApp::Run()
{
__try
{
int i = CWinApp::Run();
m_Exitok = MAGIC_EXIT_NO;
return i;
}
__finally
{
if (m_Exitok != MAGIC_EXIT_NO)
FaultHandler();
}
}