如何解决valgrind错误:大小为8的无效读取

我从valgrind得到以下错误: 大小为8的读取无效

==7864==    at 0x401148: removeAreas (election_element.c:189)
==7864==    by 0x400C1D: electionRemoveAreas (election.c:228)
==7864==    by 0x40151E: main (tests.c:34)
==7864==  Address 0x52040f8 is 24 bytes inside a block of size 32 free'd
==7864==    at 0x4C2ACBD: free (vg_replace_malloc.c:530)
==7864==    by 0x400EBF: removeElement (election_element.c:86)
==7864==    by 0x40113F: removeAreas (election_element.c:187)
==7864==    by 0x400C1D: electionRemoveAreas (election.c:228)
==7864==    by 0x40151E: main (tests.c:34)
==7864==  Block was alloc'd at
==7864==    at 0x4C29BC3: malloc (vg_replace_malloc.c:299)
==7864==    by 0x400C94: createElectionElement (election_element.c:16)
==7864==    by 0x400DA2: addElectionElement (election_element.c:48)
==7864==    by 0x400951: electionAddArea (election.c:112)
==7864==    by 0x40146A: main (tests.c:14)

i used the function removeElement for different things and it doesn't give any error, and it's too long so I won't put it here. so here is my struct:

typdef struct election_element
{
    int id;
    char* name;
     Vote votes; 
     struct election_element *next;

}*Election_element;

this is the function removeAreas:

Election_element removeAreas(Election_element area,AreaConditionFunction should_delete_area)
{
  Election_element area_ptr=area;
  while (area_ptr)
  {
    if(should_delete_area(area_ptr->id))
    {
     area=removeElement(area,area_ptr->id);
    }
   area_ptr=area_ptr->next;
  }
  return area;
}

and here is electionRemoveAreas:

ElectionResult electionRemoveAreas(Election election,AreaConditionFunction should_delete_area)
{
  if(election==NULL)
  {
    return ELECTION_NULL_ARGUMENT;
  }
  election->area=removeAreas(election->area,should_delete_area);
  return ELECTION_SUCCESS;
}
评论