views:

117

answers:

3

Can you tell me is there any thing wrong in the code, my application is crashing randomly I cannot find any possible logical or memory error, please help as this is going out of my scope.

#define __FN__ "CGD9_gd_ParseAddFieldsProC"
int CGD9_gd_ParseAddFieldsProC (CGD_gd_ParseAddFields_Iparam_t *i_param_st_p)
{

   t_gd9adfld_t *p_ext_fields_s = NULL;
   t_gd9sbdat_t *p_buff =
                  ( t_gd9sbdat_t * )( i_param_st_p->i_add_fields_st );

   Trace(__FN__);

   DEBUG_GD_1("\n\n Function %s - started. \n\n", __FN__);

   if(*(i_param_st_p->o_add_fields) == NULL) {
     ERR_Report_1(DGD_ERR_IN_FUNCTION,
     __FN__ "ERROR - program will crash, input extended struct\
has not been initialized!");

     ERR_Report_1(DGD_ERR_IN_FUNCTION, __FN__\
"Check that exit point CMI9_auxc_A_GUIDING_init_forProc \
is used in GD table!");

    fflush(NULL);
    return FAILURE;
   }

   p_ext_fields_s = *(i_param_st_p->o_add_fields);

   memset ( p_ext_fields_s, ' ', sizeof (t_gd9adfld_t));

/* Copy all extended fields from GD tables to buffer one by one*/

        memcpy(&p_ext_fields_s->rowid,
          &p_buff[i_param_st_p->output_index].rowid,
          sizeof(p_buff[i_param_st_p->output_index].rowid));

    memset(p_ext_fields_s->rowid,'0',18);


        memcpy(&p_ext_fields_s->l9_legacy_prod_type,
          &p_buff[i_param_st_p->output_index].l9_legacy_prod_type,
          sizeof(p_ext_fields_s->l9_legacy_prod_type));


   /* Free the memory allocated for extended fields */

   free(i_param_st_p -> i_add_fields_st);
   i_param_st_p -> i_add_fields_st = NULL;

   DEBUG_GD_1("\n\n Function %s - completed successuflly. \n\n", __FN__);

   return SUCCESS;
}
+3  A: 

Shot in the dark:

You're using

memcpy(&p_ext_fields_s->rowid ...

but

memset(p_ext_fields_s->rowid,...

So perhaps it should be

memset(&p_ext_fields_s->rowid,

instead? But if this really was the problem, I would expect it to not randomly crash, but crash every single time...

Ben Schwehn
it is crashing every single time. but some times it run for 3 hrs and some times only for 2 min
Kimi
A: 

Why do you use sizeof() in the first case and then constant size (18) in the second?

memcpy(&p_ext_fields_s->rowid,
  &p_buff[i_param_st_p->output_index].rowid,
  sizeof(p_buff[i_param_st_p->output_index].rowid));

memset(p_ext_fields_s->rowid,'0',18);

PS I'd propose run your code with memory checking tool (as I suspect memory corruptions).

Pmod
one thing i want to confirm looking at the above post should the code be like this: memset(
Kimi
Kimi
Why didn't you correct the same in question?
Pmod
its still crashing ... :(
Kimi
Try excluding lines step by step. Of course, you'll miss some functionality, but you'll be able to identify the failing piece of code. For example flush(null) can be dropped, free() calls can be dropped (of course, it'll be a memory leak, but it might help to find the reason of crashing).
Pmod
A: 

The code is crashing to free memory allocation:

free(i_param_st_p -> i_add_fields_st); 

What is wrong in tht :< ?

Joice