Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Simulink integration issue

102 views
Skip to first unread message

Ádám Boronyák

unread,
Jan 31, 2025, 4:14:14 AMJan 31
to Protocol Buffers
Hi,

I would like to integrate and use protobuf C++ library in Simulink S-functions on Windows 10. For this purpose I have installed protobuf 3.21.12 using Visual Studio 2022 via vcpkg. I have created a simple Simulink model (attached to the email) with an S-function builder. In this S-function builder I set a library path to vcpkg lib folder and an include path to vcpkg include folder to be able to find protobuf headers and libraries. I set there also the libprotobuf.lib as an entry. In environment variables I set path to the vcpkg bin folder because libprotobuf.lib links to the corresponding dll file. So every link and entry have been set for the project.
In my S-function in wrapper.cpp file (attached to the email), I create an instance of the built-in Timestamp message, set the seconds field to 2 and give it to the output of the S-function. After I compile the S-function or try to run the Simulink model, Simulink freezes and after some time I get a "could not find a dll" like issue which does not help to debug the issue.
I created a contact with Mathworks and they suggested a way to debug this. So I opened Visual Studio, opened the wrapper file, set a break point to the instance creation and attached the Matlab process to it. After I try to run my Simulink model I get an access violation exception. I have attached a screenshot about it.
Mathworks suggested that I should contact you to help troubleshooting this issue. What is your opinion about it? What can cause this issue?

Regards,
Adam
test_model.slx
matlab_exception_thrown.PNG
proto_test_wrapper.cpp

Adam Cozzette

unread,
Jan 31, 2025, 5:25:00 PMJan 31
to Ádám Boronyák, Protocol Buffers
It's hard to say what the root cause is, but the error message suggests that a global mutex pointer is null when it should not be.

My best guess would be that your setup is doing something unsupported with dynamic linking, such as perhaps unloading libprotobuf and reloading it again. The protobuf library has some global mutable state (including that mutex pointer) which I suspect would cause problems with unloading the library and loading it again.

Another thing is that 3.21.12 is a somewhat old release and not officially supported anymore, so you might want to upgrade to a newer version as a first step.

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+u...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/protobuf/5dcf267f-0d17-4ff5-8c73-f7eb3acc55b8n%40googlegroups.com.

Michael Ngarimu

unread,
Jan 31, 2025, 9:40:40 PMJan 31
to Protocol Buffers
This does not look like a protobuf issue to me. Is it possible this is simply an issue that you haven't told Matlab how to find the protobuf DLL? 

Where is the "could not find dll like" issue?

Beyond the scope of this list, I have ,
1. used vcpkg to acquire protobuf 3.21.12,
2. taken your model and modified the LIB_PATH and INC_PATH to point to location where vcpkg installed the protobuf headers and built libs,
3. rebuilt your s-function

The only issue I ran into is Matlab S-Function builder gave a vague error that it could not obtain sizes from the MEX file.... 

but that was because the MEX failed to build completely... 

but that was because Matlab doesn't know where to find the protobuf DLL. 

How are you making the protobuf DLL available to Matlab?

When I correctly provide the protobuf DLL to Matlab, I can see the value you assign and retrieve through the protobuf::Timestamp (the value, 2) in the outputs from your model.
Screenshot 2025-01-31 183750.png

Ádám Boronyák

unread,
Feb 1, 2025, 4:31:02 AMFeb 1
to Protocol Buffers
Hi,

Thanks for your explanations. I only add the path to the protobuf dll to the PATH environment variable. I read that it should be enough for Matlab to find the dll.
Michael Ngarimu what do you mean by "correctly provide the protobuf DLL to Matlab"? How did you do it?

Regards,
Adam

Michael Ngarimu

unread,
Feb 1, 2025, 9:00:10 PMFeb 1
to Protocol Buffers
[Since this is off-topic, I'm happy to take this off-list of mods deem it necessary].

While, "yes" adding the directory containing the DLL to the PATH env var is one way the Windows dynamic loading can find a DLL.

I feel like you might not be aware of - or not considering - how environments in processes work. There is the question of how you updated the PATH variable, i.e., it depends on the process where you updated the environment (e.g., an existing instance of Powershell, the login instance of explorer.exe, or somewhere else) and whether or not Matlab was running and restarted.

Although you've updated the PATH, could it have been while Matlab was running and has not been restarted? Or, you may have updated the PATH in a running cmd.exe shell with a simple PATH=..., or in a running Powershell instance with $env:PATH=... Starting a new copy of Matlab from the Start Menu where the explorer.exe is unaware of the change made will inherit an environment from explorer.exe that does not contain your change. Maybe the PATH was updated through Control Panel or Settings.app but Matlab was running at the time and has its own copy of the environment. 





Ádám Boronyák

unread,
Feb 3, 2025, 3:44:28 AMFeb 3
to Protocol Buffers
Hi,

I edited the environment variables via Control Panel before Matlab was running. In Matlab command window I typed getenv('path') and I see the bin folder to the protobuf DLL which I have added.
I tried now four things:
  • Add protobuf DLL path to the beginning of system path variable, not the user path variable.
  • Add protobuf DLL path as a LIB_PATH in S-function builder.
  • Add system path variable directly as an ENV_PATH in S-function builder.
  • Copy the DLL next to the model in the same folder.
None of them have been worked for me, I get the same error (Error while obtaining sizes from mex s-function).
When I run the simulation, I also see an "Invalid mex file, the specified module not found" error (not a DLL not found error, which I wrote earlier).
I do not know what I am doing wrong. Michael Ngarimu how could you make my model work? What was your settings to provide the DLL to Matlab?

Regards,
Adam

Reply all
Reply to author
Forward
0 new messages