Segmentation violation on integrating an C function using Legacy Code Tool

3 ビュー (過去 30 日間)
Thomas Trefflich
Thomas Trefflich 2016 年 5 月 24 日
回答済み: jiayu tu 2019 年 12 月 9 日
Hello,
i need to call a dynamic link library in simulink. To do that i wrote a c function for calling the dll and I'm using LCT for creating an simulink block. Everything works fine until i start the simulation with my created simulink block. Following error occurs:
------------------------------------------------------------------------
Segmentation violation detected at Tue May 24 14:28:01 2016
------------------------------------------------------------------------
Configuration:
MATLAB Version: 7.4.0.287 (R2007a)
MATLAB License: 236337
Operating System: Microsoft Windows XP
Window System: Version 5.1 (Build 2600: Service Pack 3)
Processor ID: x86 Family 6 Model 15 Stepping 2, GenuineIntel
Virtual Machine: Java 1.5.0_07 with Sun Microsystems Inc. Java HotSpot(TM) Client VM mixed mode
Default Charset: windows-1252
Register State:
EAX = 00000000 EBX = 41e00000
ECX = 16a03280 EDX = 17188a50
ESI = 17188e70 EDI = 15848590
EBP = 00ceb0f0 ESP = 00ceb018
EIP = 7a7fbde9 FLG = 00010206
Stack Trace:
[0] libmwsimulink.dll:struct slErrMsgQueue * __cdecl CMexSFcnOutputFcn(struct slSFcnInfo *)(0x15848590, 0x16a02f40, 0x17188e70, 0x16a45020) + 1417 bytes
[1] libmwsimulink.dll:struct slErrMsgQueue * __cdecl SFcnOutputFcn(class slBlock *,class slSimBlock_tag *)(0x01a02f40, 0x12feef00, 0x00ceb180 "°±Î", 0x7a4df976) + 315 bytes
[2] libmwsimulink.dll:public: virtual struct slErrMsgQueue * __thiscall LegacyDirectRunTimeDispatcher::Output(class slSimBlock_tag *)(0x12feef00, 0, 0x16a45020, 0x18618210) + 16 bytes
[3] libmwsimulink.dll:struct slErrMsgQueue * __cdecl ExecSTaskOutputList(class slModel *,struct bdCompInfo_tag *,struct bdExecInfo_tag *)(0x16a45020, 0x18618210, 0x15848d60, 0x16a45020) + 438 bytes
[4] libmwsimulink.dll:struct slErrMsgQueue * __cdecl BdSystemOutput(class slModel *,struct bdCompInfo_tag *,struct bdExecInfo_tag *)(0x00a45020, 0x18618210, 0x15848d60, 0x16a45020) + 287 bytes
[5] libmwsimulink.dll:public: virtual struct slErrMsgQueue * __thiscall SlNormalModelFcns::outputFcn(class slModel *)(0x00a45020, 0x16a45020, 0, 0x17188a50) + 103 bytes
[6] libmwsimulink.dll:struct slErrMsgQueue * __cdecl ModelOutput(class slModel *)(0x00a45020, 0xc34f5893, 0x17188a50, 0x16a45020) + 320 bytes
[7] libmwsimulink.dll:struct slErrMsgQueue * __cdecl slDoOutputAndUpdate(class slModel *)(0x00a45020, 0x00ceb294, 0, 0) + 682 bytes
[8] libmwsimulink.dll:struct slErrMsgQueue * __cdecl StepMenuSim(struct slBlockDiagram_tag *)(0x008937d0, 0, 0x168937d0, 0x00ceb298 "Ô²Î") + 223 bytes
[9] libmwsimulink.dll:struct slErrMsgQueue * __cdecl sleModelExecutionInterface(struct slBlockDiagram_tag *,enum slEngineCmdOption,enum ECStatus *)(0x168937d0, 5, 0x00ceb294, 0x1695bbe4) + 146 bytes
[10] libmwsimulink.dll:void __cdecl sluiWorkProcSimStep(void *)(0x168937d0, 0x7c80934a, 0x00632c0f, 0x791d01ef) + 27 bytes
[11] uiw.dll:void __cdecl UIW_CallWorkProcs(void)(0x00ceb2f0, 0x7921d501, 0, 0x7921d508) + 42 bytes
[12] uiw.dll:char * __cdecl getCommand(char *,int)(0x7a032920, 4096, 0x00ceb380, 0x7a024229) + 98 bytes
[13] uiw.dll:public: virtual char * __thiscall uiw::UIW_IOProxy::getCmdWindowCommand(char *,int)const (0x7a032920, 4096, 0xfb306720, 0x00ceb3f0) + 16 bytes
[14] bridge.dll:char * __cdecl win32IoReadLine(bool,struct _iobuf *,char *,char *,int,bool *)(0x781c1b70, 0x120abb20, 4096, 0x00ceb3c3) + 185 bytes
[15] bridge.dll:char * __cdecl ioReadLine(bool,struct _iobuf *,char *,char *,int,bool *)(0, 0x781c1b70, 0x120abb20, 0x120abb20) + 24 bytes
[16] bridge.dll:void __cdecl mnGetFullLine(char * *,unsigned int *,unsigned int *,bool)(0x00ceb3ec, 0x00ceb3e4, 0, 0x7848c6b0) + 91 bytes
[17] bridge.dll:_mnGetCommandLineBuffer(0, 0xfb302024, 0x01801718, 0x018016b8) + 131 bytes
[18] bridge.dll:__catch$_mnParser$0(0xfb33fec0, 0x018016b8, 0x018016b8, 0) + 144 bytes
[19] mcr.dll:public: void __thiscall mcrInstance::mnParser(void)(0xfb33f5cc, 0x004074a4, 336704, 0) + 62 bytes
[20] MATLAB.exe:0x004021b8(4194304, 0, 336704, 10)
[21] MATLAB.exe:0x00403bd2(1109972, 0, 0x7ffd5000, 0)
[22] kernel32.dll:0x7c816037(0x00403daf, 0, 0x78746341, 32)
Please follow these steps to report this problem to The MathWorks so we
have the best chance of correcting it:
The next time MATLAB is launched under typical usage, a dialog box will
open to help you send the error log to The MathWorks. Alternatively, you
can send an e-mail to segv@mathworks.com with the following file attached:
C:\DOKUME~1\ttr\LOKALE~1\Temp\matlab_crash_dump.444
If the problem is reproducible, please submit a Service Request via:
http://www.mathworks.com/support/contact_us/ts/help_request_1.html
A technical support engineer might contact you with further information.
Thank you for your help. Save your workspace and restart MATLAB.
-------------------------------------------------------------
This segmentation violation occurred while executing the
S-function 'call_dll' in block 'untitled/call_dll'.
A common cause of this segmentation violation is an incorrect
input port direct feedthrough setting. Each input port of the
S-function that is read (accessed) in mdlOutputs and/or
mdlGetTimeOfNextVarHit must specify that it needs its input
signal in these routines by setting direct feedthrough for
these input ports.
1) To debug your C-MEX S-function, you can enable diagnostics
by compiling the S-function source with the -g flag, e.g.,
mex -g sfunction_name.c
2) You can ask Simulink to try assuming your S-function has
direct feedthrough using:
set_param('modelname','TryForcingSFcnDF','on')
If Simulink can find a valid sorting mode that does not
result in algebraic loops involving your S-function, your
model will execute (assuming that the cause of this
segmentation violation is an incorrect direct feedthrough
setting on an input port).
See matlabroot/simulink/src/sfuntmpl_directfeed.txt
-------------------------------------------------------------
Here my c function:
#include "windows.h"
#include "simstruc.h"
#include "m5.h"
#include "stdio.h"
double open_device(double u)
{
typedef UINT (CALLBACK* LPFNDLLFUNC1)(DWORD,CHAR*);
double y=0;
HINSTANCE hDLL; // Handle to DLL
LPFNDLLFUNC1 lpfnDllFunc1; // Function pointer
char uParam2[]="Init_String";
hDLL = LoadLibrary("m5apiw32.dll");
if (hDLL != NULL)
{
lpfnDllFunc1 = (LPFNDLLFUNC1)GetProcAddress(hDLL,"PCube_getModulesIdMap");
if (!lpfnDllFunc1)
{
// handle the error
FreeLibrary(hDLL);
}
else
{
// call the function
y = lpfnDllFunc1(u, uParam2);
return y;
}
}
}
and my LCT options:
def = legacy_code('initialize');
def.SourceFiles = {'lct.c'};
def.OutputFcnSpec = 'double y1 = open_device(double u1)';
def.SFunctionName = 'call_dll';
legacy_code('sfcn_cmex_generate', def);
legacy_code('sfcn_tlc_generate', def);
legacy_code('compile', def);
legacy_code('slblock_generate', def);
Best Regards and thanks for the help
Thomas

回答 (1 件)

jiayu tu
jiayu tu 2019 年 12 月 9 日
I had the same problem, did you solve it?

カテゴリ

Help Center および File ExchangeBlock and Blockset Authoring についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by