C S-function problem when trying to get a pointer to a structure

2 visualizaciones (últimos 30 días)
Hello all,
I created a structure within the mdlStart method and a pointer to it as follows:
SomeStructureType myStruct;
SomeStructureType* myStruct_ptr;
myStruct_ptr = &myStruct;
Then I saved the pointer into a (previously defined) PWork vector:
ssSetPWorkValue(S, 0, (void *)myStruct_ptr);
When I retrieve the pointer, within mdlStart, as follows:
anotherStruct_ptr= (SomeStructureType*)ssGetPWorkValue(S,0);
everything works fine. anotherStruct_ptr points to myStruct. But when I try to get the same pointer from the mdlTerminate method, I obtain a wrong one:
SomeStructureType *Test_ptr;
Test_ptr = (SomeStructureType*)ssGetPWorkValue(S,0);
Test_ptr does not point to myStruct.
Avoiding the cast gives the same result. I also tried to use ssSetUserData and ssGetUserData instead of ssSetPWorkValue and ssGetPWorkValue, respectively, but the result is still wrong.
I already used PWork vectors without any problem, but this is the first time I use it to hold a pointer to a structure. Am I doing something wrong?

Respuesta aceptada

Kaustubha Govind
Kaustubha Govind el 24 de Oct. de 2012
You need to use:
SomeStructureType* myStruct_ptr = new SomeStructureType;
to create the pointer. If you simply assign it to a variable on the stack, that memory is free'd at the end of mdlStart, and any further references to it are pointing to corrupt memory and can cause a segmentation violation.
Also, remember to use delete to free that memory in mdlTerminate.
  1 comentario
Leonardo
Leonardo el 25 de Oct. de 2012
Editada: Leonardo el 25 de Oct. de 2012
I completely forgot to allocate the structure!! Thanks!

Iniciar sesión para comentar.

Más respuestas (1)

Pedro Oliveira
Pedro Oliveira el 28 de Abr. de 2014
Im having a simular problem.
In the mdlStart im doing
{
SomeStructureType* myStruct_ptr = new SomeStructureType("Test",0);
ssSetPWorkValue(S, 0, (void *)myStruct_ptr);
void * vptr =ssGetPWorkValue(S,0);
SomeStructureType *Test_ptr;
Test_ptr = (SomeStructureType*)vptr;
ssPrintf("%s=%d\n",Test_ptr->info,Test_ptr->counter);
}
The print out is
Test 0
In the mdlInitializeConditions or mdlUpdate
{
void * vptr =ssGetPWorkValue(S,0);
SomeStructureType *Test_ptr;
Test_ptr = (SomeStructureType*)vptr;
ssPrintf("%s=%d\n",Test_ptr->info,Test_ptr->counter);
}
This gives a segmentation fault.
By looking to the compiler debug attached to Matlab vptr is the correct location (0x00D3A6F0, eg) when we jump between functions but the "recovery" of the class is comletely corrupted.
Class SomeStructureType
{
public:
SomeStructureType(std::string _info,int _counter);
std::string info;
int counter;
}
SomeStructureType::SomeStructureType(std::string _info,int _counter)
{
info = _info;
counter = _counter;
}
Am I doind something wrong, theres any thing to do with multithread?
Im using MS Visual Studio 2008 express edition and Matlab 2008a

Categorías

Más información sobre Simulink en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by