COleException at memory location for COleSafeArray - c++

I'm trying to work with the following sample code that was provided to me
// select the Access levels from access level list
BSTR myBstr = SysAllocString(L"Hello World");
COleSafeArray saAccessLevels;
VARIANT vAccls;
saAccessLevels.CreateOneDim(VT_BSTR, 1);
saAccessLevels.PutElement(0, myBstr); // <-- Error after this line
vAccls = saAccessLevels.Detach();
The Visual C++ debugger breaks at the line noted in the above comment and I get the following errors:
f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\olemisc.cpp(423) : AppMsg - Warning: constructing COleException, scode = E_INVALIDARG ($80070057).
First-chance exception at 0x75371D4D in MyApplication.exe: Microsoft C++ exception: COleException at memory location 0x0057FC58.
Unhandled exception at 0x75371D4D in MyApplication.exe: Microsoft C++ exception: COleException at memory location 0x0057FC58.
I am not familiar with C++ programming. What am I doing wrong?

You're using the PutElement method incorrectly. The first parameter is a pointer to an array of indices. Have a look at MSDN.

Related

Visual Studio Graphics Debugger throws read access violation exception

I'm writing a simple renderer using the d3d11 library in Visual Studio 2019 and it builds and runs fine. However when I try running the Graphics Debugger it immediately throws a read access violation for address 0x0000000000000000 ( which is clearly incorrect ).
The exception is thrown from the DXCaptureReplay dll on the line
DeviceContext.PSSetShader(InShaderToBind.Shader.PS, NULL, 1);
Where InShaderToBind.Shader.PS is a pointer to ID3D11PixelShader
It got the most weird when I out of a lack of ideas tried
int X = 0;
ID3D11ClassInstance* FakedClassInstance = reinterpret_cast<ID3D11ClassInstance*>(&X);
DeviceContext.PSSetShader(InShaderToBind.Shader.PS, &FakedClassInstance, 1);
As this will make the exception not throw until I try to capture a frame ( Which I guess makes sense as that pointer will only be valid for the scope where X is still valid )
The MSDN documentation states that NULL should be a perfectly valid argument to pass to PSSetShader ( as noted here: https://learn.microsoft.com/en-us/windows/win32/api/d3d11/nf-d3d11-id3d11devicecontext-pssetshader )
Any ideas for what might be going wrong?
( If I comment out PSSetShader the exception is not thrown and I can take captures )
If you enable the Direct3D Debug Device, you would see in your debug output window:
D3D11 CORRUPTION: ID3D11DeviceContext::PSSetShader: Second parameter (ppClassInstances) corrupt or unexpectedly NULL. [ MISCELLANEOUS CORRUPTION #14: CORRUPTED_PARAMETER2]
NULL (or better yet nullptr) is fine for ppClassInstances only if NumClassInstances is 0. Try:
DeviceContext.PSSetShader(InShaderToBind.Shader.PS, NULL, 0);
Generally you should make sure your program runs without emitting ERROR or CORRUPTION messages from the debug layer before attempting to use PIX or the VSGS tool.
See Microsoft Docs and this blog post.

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.

Strange crash with exceptions

When debugging a C++ program in VS 2013, I get a crash (technically a "breakpoint") in an exception handler (catch block) after an exception is thrown. The following is from the Output window after it happens:
First-chance exception at 0x75E54878 in Program.exe: Microsoft C++ exception: _com_error at memory location 0x00C6EE24.
First-chance exception at 0x75E54878 in Program.exe: Microsoft C++ exception: ComException at memory location 0x00C6EF04.
First-chance exception at 0x75E54878 in Program.exe: Microsoft C++ exception: [rethrow] at memory location 0x00000000.
HEAP[Program.exe]: HEAP: Free Heap block 00FAB5A8 modified at 00FAB5D4 after it was freed
Program.exe has triggered a breakpoint.
I tweaked the code to find the cause and got it to the point that it does something as simple as displaying a message box. Something like:
try
{
...
}
catch (const Exception&)
{
::MessageBox(NULL, L"Error", L"Error", 0); // <-- Crash happens here,
// BEFORE the message box is displayed
}
The message from the Output window suggests that I'm trying to write to freed/invalid memory, but as you can see from this code snippet, that's not the case - I'm not even using variables at the time the crash happens.
Another strange thing is that, after the crash, the Call Stack window shows frames that were already unwound due to the exception, as if the call stack got "rebuilt".
Any ideas what's wrong?

Unhandled exception while assigning strings. 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.

std::string crashing while iterating on mySQL connector query results

I'm trying to get a std::string from my database using mysql connexion
Here is the simple code:
sql::Statement *stmt = con->createStatement();
sql::ResultSet *res = stmt->executeQuery("SELECT * FROM test.new_table");
while (res->next()) {
std::string somestring = res->getString("idnew_table");
} //crashes here
delete res;
delete stmt;
So, the executeQuery is fine, I enter the loop, and if I break, the expected results are in somestring. After the somestring declaration, I step foward to the end of the loop, and it crashes before the next iteration!
Here is the call stack:
> msvcp100d.dll!std::_Lockit::_Lockit(int kind) Line 64 + 0x14 bytes C++
msvcp100d.dll!std::_Container_base12::_Orphan_all() Line 200 C++
CM.dll!std::_String_val<char,std::allocator<char> >::~_String_val<char,std::allocator<char> >() Line 478 + 0xb bytes C++
CM.dll!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::~basic_string<char,std::char_traits<char>,std::allocator<char> >() Line 755 + 0xf bytes C++
CM.dll!DAL::GetInfo() Line 45 + 0xc bytes C++
Output:
First-chance exception at 0x1038ad4a (msvcp100d.dll) in CMLauncher.exe: 0xC0000005: Access violation reading location 0xccccccd0.
Unhandled exception at 0x76f615de in CMLauncher.exe: 0xC0000005: Access violation reading location 0xccccccd0.
First-chance exception at 0x76f5016e in CMLauncher.exe: 0x00000000: The operation completed successfully.
Unhandled exception at 0x76f615de in CMLauncher.exe: 0x00000000: The operation completed successfully.
So it looks like I have some uninitialized memory somewhere in the C++ runtime lib...
It look like it's crashing in the std::string destructor, which kind of makes sense because it crashes when the scope of the string is done...
My best guess is that libmysql is using an older version of the C++ runtime (say msvcp90d.dll) and that it's clashing with the new one... does that even make sense?
I'm under windows 7, using mySQL Server 5.5, VS2010 Pro. all in 32bits. thx! I'll be happy to post any mroe information that is needed.
Edit: Before anyone else reads DumbCoders comment:
MySQL Connector example
The documentation specifies that both the statement and the resultSet have to be deleted.
This problem seems just like yours here.