cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Did you get called away in the middle of writing a post? Don't worry you can find your unfinished post later in the Drafts section of your profile page. X

User Defined Struct Crashing Creo to Fatally Crash

MH_10366957
10-Marble

User Defined Struct Crashing Creo to Fatally Crash

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

ACCEPTED SOLUTION

Accepted Solutions
FV
17-Peridot
17-Peridot
(To:MH_10366957)

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

View solution in original post

6 REPLIES 6

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.

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

FV
17-Peridot
17-Peridot
(To:MH_10366957)

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

MH_10366957
10-Marble
(To:FV)

Thank you for the help, I rewrote it doing what you suggested and it worked as it was supposed to.

FV
17-Peridot
17-Peridot
(To:MH_10366957)

not that it matters but why not to use the STL containers instead of K&R circa self-referential structures?

MH_10366957
10-Marble
(To:FV)

It made some logic further down the pipeline easier to design.

Announcements


Top Tags