Crash during initialization of c++ clib object.

14 次查看(过去 30 天)
Hello All,
I am building a custom visual studio dll and I generate a clib interface to it using the interfaceGeneration routines.
I am using MATLAB 2023a with visual studio 2019.
I have a custom c++ class (called DeviceInfo) that is a inherted class from a simple HardwareInfo class.
Internally (in the cpp) this class checks some shared pointers that the DeviceInfo object owns and free them if nessecary in its destructor. (that are all alocated internally in the cpp) among these are thread objects.
If I build the dll in RelWithDebInfo. I can use visual studio to attach to the matlabclibhost and do some basic debugging. I am getting seg faults sometimes when I create/delete a DeviceInfo object from matlab. I also am getting heap corruption error from the visual studio debugger.
Critical error detected c0000374
A breakpoint instruction (__debugbreak() statement or a similar call) was executed in MATLABCLibHost.exe.
Exception thrown at 0x00007FFFFCF1C7A9 (ntdll.dll) in MATLABCLibHost.exe: 0xC0000374: A heap has been corrupted (parameters: 0x00007FFFFCF938A0).
Unhandled exception at 0x00007FFFFCF1C7A9 (ntdll.dll) in MATLABCLibHost.exe: 0xC0000374: A heap has been corrupted (parameters: 0x00007FFFFCF938A0).
Exception thrown: read access violation.
**_Left** was 0xFFFFFFFFFFFFFFFF.
usage MATLAB code:
testDevice = clib.ImatestControl.ControlAPI.DummyDeviceInfo
testDevice = clib.ImatestControl.ControlAPI.DummyDeviceInfo %<---destructor should be called on the 1st instance here
Any Help/Hints would be appreciated.
Thanks,
Trevor

采纳的回答

Rishi
Rishi 2024-2-27
Hi Trevor,
From your question, I understand that you have created a class called 'DeviceInfo' and are encountering errors when attempting to create or delete an object of that class.
From the stack trace you have provided, it appears that the error you are facing is due to heap corruption. Heap corruption usually occurs when you are trying to write to memory outside its bounds, or when you free memory more than once. Heap corruption is generally not caused by the line of code where it is detected. Check for buffer overruns and other places where you might be deleting memory (or writing to memory; however, based on your question, it seems the former is the issue in your case).
A similar issue can be found below in the 'cplusplus' forum:
Hope this helps!
  1 个评论
Trevor
Trevor 2024-2-27
Hi Rishi,
Thanks for the reply. Turns out this issue was because the headers I was using for clib generation were different than what the dll library was built with (I was restricting some member variables/functions from being parsed by the generation which I am guessing essentially made the data structure sizes mismatch during runtime. - causing heap errors) If I use the headers as they are used to build the dll - things seem to be stable.
Thanks,
-Trevor

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Call C++ from MATLAB 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by