From: Jörn on
Hi,

I've a problem with making a c++ object persistent in a c++ s-function.
I tried it on different ways, but Matlab always crashes...

In the mehtod "mdlInitializeSizes" I do "ssSetNumPWork(S, 1)" to initialize a PWork for the object. In the mdlStart method I tried the following:

void ** tmp = ssGetPWork(S);
tmp[0] = new ExampleProcess(X,Y,Z);

AND

void ** tmp = ssGetPWork(S);
ExampleProcess * process = new ExampleProcess(X,Y,Z);
tmp[0] = process;

Both versions don't work. With the first version Matlab crashes after creating the object and with the second version Matlab crashes if I try to use the object in the mdlOutputs method like that:

ExampleProcess * process = (ExampleProcess *)ssGetPWorkValue(S, 0);


So, how to use a PWork properly? The ExampleProcess-class has a constructor with three parameters, which are represented here with X, Y and Z. I use the MinGW compiler in combination with gnumex to compile the s-function.

Regards, Joern
From: Praetorian on
On Jun 28, 11:09 am, "Jörn " <maili...(a)hardware-datenbank.de> wrote:
> Hi,
>
> I've a problem with making a c++ object persistent in a c++ s-function.
> I tried it on different ways, but Matlab always crashes...
>
> In the mehtod "mdlInitializeSizes" I do "ssSetNumPWork(S, 1)" to initialize a PWork for the object. In the mdlStart method I tried the following:
>
> void ** tmp = ssGetPWork(S);
> tmp[0] = new ExampleProcess(X,Y,Z);
>
> AND
>
> void ** tmp = ssGetPWork(S);
> ExampleProcess * process = new ExampleProcess(X,Y,Z);
> tmp[0] = process;
>
> Both versions don't work. With the first version Matlab crashes after creating the object and with the second version Matlab crashes if I try to use the object in the mdlOutputs method like that:
>
> ExampleProcess * process = (ExampleProcess *)ssGetPWorkValue(S, 0);
>
> So, how to use a PWork properly? The ExampleProcess-class has a constructor with three parameters, which are represented here with X, Y and Z. I use the MinGW compiler in combination with gnumex to compile the s-function.
>
> Regards, Joern

Joern,
Try this:

void * tmp = ssGetPWork(S);
tmp[0] = new ExampleProcess(X,Y,Z);

ssGetPWork(S) returns a void *, not a pointer to a void *, this extra
level of dereferencing will cause segmentation faults. Also, make sure
you're deleting the C++ object in mdlTerminate and that you've set
SS_OPTION_CALL_TERMINATE_ON_EXIT using ssSetOptions().

Regards,
Ashish.
From: Jörn on
The matlab doc says that it is a void **. And with a single pointer the code doesn't compile.
http://www.mathworks.com/access/helpdesk/help/toolbox/simulink/sfg/bre1pj2.html

"Jörn " <mailings(a)hardware-datenbank.de> wrote in message <i0akvj$kd9$1(a)fred.mathworks.com>...
> Hi,
>
> I've a problem with making a c++ object persistent in a c++ s-function.
> I tried it on different ways, but Matlab always crashes...
>
> In the mehtod "mdlInitializeSizes" I do "ssSetNumPWork(S, 1)" to initialize a PWork for the object. In the mdlStart method I tried the following:
>
> void ** tmp = ssGetPWork(S);
> tmp[0] = new ExampleProcess(X,Y,Z);
>
> AND
>
> void ** tmp = ssGetPWork(S);
> ExampleProcess * process = new ExampleProcess(X,Y,Z);
> tmp[0] = process;
>
> Both versions don't work. With the first version Matlab crashes after creating the object and with the second version Matlab crashes if I try to use the object in the mdlOutputs method like that:
>
> ExampleProcess * process = (ExampleProcess *)ssGetPWorkValue(S, 0);
>
>
> So, how to use a PWork properly? The ExampleProcess-class has a constructor with three parameters, which are represented here with X, Y and Z. I use the MinGW compiler in combination with gnumex to compile the s-function.
>
> Regards, Joern
From: Jörn on
Has somebody another suggestion? I have still the same problem...

Regards, Joern
From: Phil Goddard on

Have you looked at the example:

>> sfcndemo_counter_cpp

It shows how to create, store and retrieve a custom C++ class using PWork in an S-Function.

Phil.