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

조회 수: 2 (최근 30일)
Leonardo
Leonardo 2012년 10월 24일
답변: Pedro Oliveira 2014년 4월 28일
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?

채택된 답변

Kaustubha Govind
Kaustubha Govind 2012년 10월 24일
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
Leonardo
Leonardo 2012년 10월 25일
편집: Leonardo 2012년 10월 25일
I completely forgot to allocate the structure!! Thanks!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Pedro Oliveira
Pedro Oliveira 2014년 4월 28일
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

카테고리

Help CenterFile Exchange에서 Simulink에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by