DSPRelated.com
Forums

Segmentation Violation

Started by Unknown October 7, 2001
Hello all , I have written an S function in C and it compiles that is a dll is
generated. But when I put it on the model in simulink with necessary
inputs it crashes .The error message and the c file is as follows.
Can anyone help me with this problem. Please throw some light!
Regards

Tahmid
----------------------------------
--
Segmentation violation detected at Sun Oct 07 14:32:16 2001
----------------------------------
--

Configuration:
MATLAB Version: 5.3.0.10183 (R11)
Operating System: Microsoft Windows 98
Window System: Version 4.10 (Build 1998: )

Register State:
EAX = 00000000 EBX = 00000000
ECX = 00000000 EDX = 03efa940
ESI = 03efa580 EDI = 025078a0
EBP = 01a7e4c8 ESP = 01a7e4a8
EIP = f5330000 FLG = 00010212

Stack Trace: Segmentation violation occurred within signal handler.
Unable to complete stack trace (stack was probably corrupted)
and my C file looks like this:
/*
* sfuntmpl.c: C template for a level 2 S-function.
*
* ------------------------------
-------
* | See matlabroot/simulink/src/sfuntmpl.doc for a more detailed
template |
* ------------------------------
-------
*
* Copyright (c) 1990-1998 by The MathWorks, Inc. All Rights Reserved.
* $Revision: 1.19 $
*/ /*
* You must specify the S_FUNCTION_NAME as the name of your S-function
* (i.e. replace sfuntmpl with the name of your S-function).
*/

#define S_FUNCTION_LEVEL 2
#define S_FUNCTION_NAME fluxtest
#define RS(S) ssGetSFcnParam(S,0)
#define LDS(S) ssGetSFcnParam(S,1)
#define LQS(S) ssGetSFcnParam(S,2)
#define LLS(S) ssGetSFcnParam(S,3)
#define LLR(S) ssGetSFcnParam(S,4)
/*
* Need to include simstruc.h for the definition of the SimStruct and
* its associated macro definitions.
*/
#include "simstruc.h"
/* Error handling
* --------------
*
* You should use the following technique to report errors
encountered within
* an S-function:
*
* ssSetErrorStatus(S,"Error encountered due to ...");
* return;
*
* Note that the 2nd argument to ssSetErrorStatus must be persistent
memory.
* It cannot be a local variable. For example the following will cause
* unpredictable errors:
*
* mdlOutputs()
* {
* char msg[256]; {ILLEGAL: to fix use "static char
msg[256];"}
* sprintf(msg,"Error due to %s", string);
* ssSetErrorStatus(S,msg);
* return;
* }
*
* See matlabroot/simulink/src/sfunctmpl.doc for more details.
*/

/*====================*
* S-function methods *
*====================*/

/* Function: mdlInitializeSizes
===============================================
* Abstract:
* The sizes information is used by Simulink to determine the S-
function
* block's characteristics (number of inputs, outputs, states,
etc.).
*/
static void mdlInitializeSizes(SimStruct *S)
{

int i;

/* See sfuntmpl.doc for more details on the macros below */

ssSetNumSFcnParams(S, 5); /* Number of expected parameters */
if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
/* Return if number of expected != number of actual
parameters */
return;
}

ssSetNumContStates(S, 4);
ssSetNumDiscStates(S, 0);
if (!ssSetNumInputPorts(S, 1)) return;
for(i=0;i<1;i++)
{
ssSetInputPortWidth(S, i, 12);
ssSetInputPortDirectFeedThrough(S, i, 1);
} if (!ssSetNumOutputPorts(S, 1)) return;
for(i=0;i<1;i++)
{
ssSetOutputPortWidth(S, i, 7);
}
ssSetNumSampleTimes(S, 1);
ssSetNumRWork(S, 0);
ssSetNumIWork(S, 0);
ssSetNumPWork(S, 0);
ssSetNumModes(S, 0);
ssSetNumNonsampledZCs(S, 0);

ssSetOptions(S, SS_OPTION_EXCEPTION_FREE_CODE);
}
/* Function: mdlInitializeSampleTimes
=========================================
* Abstract:
* This function is used to specify the sample time(s) for your
* S-function. You must register the same number of sample times as
* specified in ssSetNumSampleTimes.
*/
static void mdlInitializeSampleTimes(SimStruct *S)
{
ssSetSampleTime(S, 0, CONTINUOUS_SAMPLE_TIME);
ssSetOffsetTime(S, 0, 0.0);

}
#define MDL_INITIALIZE_CONDITIONS /* Change to #undef to remove
function */
#if defined(MDL_INITIALIZE_CONDITIONS)
/* Function: mdlInitializeConditions
========================================
* Abstract:
* In this function, you should initialize the continuous and
discrete
* states for your S-function block. The initial states are
placed
* in the state vector, ssGetContStates(S) or ssGetRealDiscStates
(S).
* You can also perform any other initialization activities that
your
* S-function may require. Note, this routine will be called at
the
* start of simulation and if it is present in an enabled
subsystem
* configured to reset states, it will be call when the enabled
subsystem
* restarts execution to reset the states.
*/
static void mdlInitializeConditions(SimStruct *S)
{

real_T *x = ssGetContStates(S);
x[0] = 0.51707317073;
x[1]=0.0;
x[2]=0.5;
x[3]=0.0;

}
#endif /* MDL_INITIALIZE_CONDITIONS */
#define MDL_START /* Change to #undef to remove function */
#if defined(MDL_START)
/* Function: mdlStart
=======================================================
* Abstract:
* This function is called once at start of model execution. If
you
* have states that should be initialized once, this is the place
* to do it.
*/
static void mdlStart(SimStruct *S)
{
}
#endif /* MDL_START */
/* Function: mdlOutputs
=======================================================
* Abstract:
* In this function, you compute the outputs of your S-function
* block. Generally outputs are placed in the output vector, ssGetY
(S).
*/
static void mdlOutputs(SimStruct *S, int_T tid)
{
real_T *x = ssGetContStates(S);

real_T *y = ssGetOutputPortRealSignal(S,0);
y[0]=x[0];
y[1]=x[1];
y[2]=x[2];
y[3]=x[3];
y[4]=*mxGetPr(LDS(S))*(x[0]/(*mxGetPr(LLS(S)))+x[2]/(*mxGetPr(LLR
(S))));
y[5]=*mxGetPr(LQS(S))*(x[1]/(*mxGetPr(LLS(S)))+x[3]/(*mxGetPr(LLR
(S))));

}
#define MDL_UPDATE /* Change to #undef to remove function */
#if defined(MDL_UPDATE)
/* Function: mdlUpdate
======================================================
* Abstract:
* This function is called once for every major integration time
step.
* Discrete states are typically updated here, but this function
is useful
* for performing any tasks that should only take place once per
* integration step.
*/
static void mdlUpdate(SimStruct *S, int_T tid)
{
}
#endif /* MDL_UPDATE */
#define MDL_DERIVATIVES /* Change to #undef to remove function */
#if defined(MDL_DERIVATIVES)
/* Function: mdlDerivatives
=================================================
* Abstract:
* In this function, you compute the S-function block's
derivatives.
* The derivatives are placed in the derivative vector, ssGetdX
(S).
*/
static void mdlDerivatives(SimStruct *S)
{
real_T *x = ssGetContStates(S);
InputRealPtrsType uPtrs = ssGetInputPortRealSignalPtrs(S,0); real_T *dx = ssGetdX(S);
dx[0]=U(10)-(*mxGetPr(RS(S)))*U(0);
dx[1]=U(9)-(*mxGetPr(RS(S)))*(U(1));
dx[2]=-(U(8))*x[3]-U(11)*U(2);
dx[3]=(U(8))*x[2]-U(11)*U(3); }
#endif /* MDL_DERIVATIVES */
/* Function: mdlTerminate
=====================================================
* Abstract:
* In this function, you should perform any actions that are
necessary
* at the termination of a simulation. For example, if memory was
* allocated in mdlStart, this is the place to free it.
*/
static void mdlTerminate(SimStruct *S)
{
} /*======================================================*
* See sfuntmpl.doc for the optional S-function methods *
*======================================================*/

/*=============================*
* Required S-function trailer *
*=============================*/

#ifdef MATLAB_MEX_FILE /* Is this file being compiled as a MEX-
file? */
#include "simulink.c" /* MEX-file interface mechanism */
#else
#include "cg_sfun.h" /* Code generation registration function */
#endif