Crash during initialization of c++ clib object.

26 views (last 30 days)
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

Accepted Answer

Rishi
Rishi on 27 Feb 2024
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 Comment
Trevor
Trevor on 27 Feb 2024
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

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!