Skip to main content
10-Marble
June 8, 2023
Solved

User Defined Struct Crashing Creo to Fatally Crash

  • June 8, 2023
  • 1 reply
  • 2402 views

Hello community,

 

I am working on creo 9.0.2.0

 

I am defining my own struct as this:

typedef struct ParmNote {

struct ParmNote *Next;
struct ParmNote *Last;
char Note[120];
wchar_t wcNote[120];

}NOTE_PARAM;

 

In a later function I have:

 

{

NOTE_PARAM *Current , *Next , *Last , *First;


First = (NOTE_PARAM *)(mParm->PString);
Last = First->Last;
Current = Last;
Next = (NOTE_PARAM *)malloc(sizeof (NOTE_PARAM));
Current->Next = Next;
First->Last = Next;
Current = Next;
Current->Next = NULL;
}

 

When it reaches the underlined line of code "Current->Next = Next;", creo fatally crashes. I do not understand why this is causing Creo to crash, as this exact logic worked in Creo 5.

 

Any and all help would be appreciated.

 

Thank you,

Mark Hardebeck

Best answer by FV_01

if you would rewrite code to eliminate unnecessary variable assignments and replace malloc with calloc(...) to have allocated memory initialized:

 

{
 NOTE_PARAM *first = (NOTE_PARAM *)(mParm->PString);
 //assert(NULL == first->Last);
 if( NULL != first && NULL == first->Last){
 first->Last = (NOTE_PARAM *)calloc(1, sizeof (NOTE_PARAM));
 }
 //assert(NULL != first->Last);
 //assert(NULL == first->Last->Next);
}

 

the reason for crash would be easier to detect.

HIH

1 reply

21-Topaz II
June 9, 2023

Are you sure that the data contained in mParm->PString is compatible with NOTE_PARAM?

Have you verified that the pointer First->Last is indeed valid, not NULL, and not random data created when memory was allocated? It's a good rule to always initialize pointers to NULL or the equivalent when they are created, and verification before use is imperative.

If First->Last is not a valid pointer to a properly allocated NOTE_PARAM structure, that would wreak the havoc you are seeing.

10-Marble
June 9, 2023

Yes, I have verified that mParm->PString is compatible and that First->Last is not NULL.

FV_0117-PeridotAnswer
17-Peridot
June 9, 2023

if you would rewrite code to eliminate unnecessary variable assignments and replace malloc with calloc(...) to have allocated memory initialized:

 

{
 NOTE_PARAM *first = (NOTE_PARAM *)(mParm->PString);
 //assert(NULL == first->Last);
 if( NULL != first && NULL == first->Last){
 first->Last = (NOTE_PARAM *)calloc(1, sizeof (NOTE_PARAM));
 }
 //assert(NULL != first->Last);
 //assert(NULL == first->Last->Next);
}

 

the reason for crash would be easier to detect.

HIH