Community Tip - When posting, your subject should be specific and summarize your question. Here are some additional tips on asking a great question. X
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
Solved! Go to Solution.
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
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.
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
Thank you for the help, I rewrote it doing what you suggested and it worked as it was supposed to.
not that it matters but why not to use the STL containers instead of K&R circa self-referential structures?
It made some logic further down the pipeline easier to design.