LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Glibc free() invalid pointer (https://www.linuxquestions.org/questions/programming-9/glibc-free-invalid-pointer-382836/)

pierre-luc 11-13-2005 07:44 PM

Glibc free() invalid pointer
 
I can't figure out where an error is coming from. I'm stuck writing my program getting that error:

Quote:

*** glibc detected *** free(): invalid pointer: 0x0804b410 ***
Aborted
I tried to get some more clues about this passing the whole thing to valgrind:

Code:

==10868== Invalid write of size 4
==10868==    at 0x804961B: deltaWjkMod (neurone.c:206)
==10868==    by 0x8048B66: main (main.c:67)
==10868==  Address 0x1BCD1074 is 0 bytes after a block of size 12 alloc'd
==10868==    at 0x1B8FFB95: calloc (vg_replace_malloc.c:279)
==10868==    by 0x8048C3F: wmatriceCreer (neurone.c:27)
==10868==    by 0x80489A8: main (main.c:23)
==10868==
==10868== Invalid free() / delete / delete[]
==10868==    at 0x1B8FF3D0: free (vg_replace_malloc.c:235)
==10868==    by 0x1BAACB7B: free_mem (in /lib/libc-2.3.4.so)
==10868==    by 0x1BAAC7D4: __libc_freeres (in /lib/libc-2.3.4.so)
==10868==    by 0x1B8FB6E0: _vgw_freeres (vg_preloaded.c:62)
==10868==    by 0x1B9E2585: exit (in /lib/libc-2.3.4.so)
==10868==    by 0x1B9CC470: __libc_start_main (in /lib/libc-2.3.4.so)
==10868==    by 0x80488C0: ??? (start.S:119)
==10868==  Address 0x1B913C90 is not stack'd, malloc'd or (recently) free'd
==10868==
==10868== ERROR SUMMARY: 4 errors from 2 contexts (suppressed: 42 from 3)
==10868== malloc/free: in use at exit: 0 bytes in 0 blocks.
==10868== malloc/free: 15 allocs, 16 frees, 136 bytes allocated.
==10868== For counts of detected errors, rerun with: -v
==10868== No malloc'd blocks -- no leaks are possible.

As far as GDB is concerned, I don't get much more information:
Code:

Program received signal SIGABRT, Aborted.
0x400f4ef1 in kill () from /lib/libc.so.6

The line (206) reported by valgrind is the following:
Code:

      net->wjk->wMatrice[k][i] = (net->n) * (net->cachee->delta[i]);
Inside a small function:
Code:

void
deltaWjkMod (struct reseau *net)
{
  int i, k;
  for (i = 0; i < net->nCachee + 1; i++)
    for (k = 0; k < net->nEntree + 1; k++)
      net->wjk->wMatrice[k][i] = (net->n) * (net->cachee->delta[i]);
}

What's going wrong here ? I can understand what that error could mean but I can't see what I'm not doing right.

The source code can be seen here:

http://pierreluc.aqra.ca/projet/BackPropNN/

dmail 11-13-2005 08:09 PM

i dont know if this is the answer but from what i see your func
Code:

void
deltaWjk (struct reseau *net)
{
  int i, j, k;
  float sum = 0;

  for (j = 0; j < net->nCachee + 1; j++)
    {
      sum = 0;
      for (i = 0; i < net->nSortie; i++)
        sum += net->sortie->delta[i] * net->wij->wMatrice[j][i];
      for (k = 0; k < net->nEntree + 1; k++)
        net->cachee->delta[j] +=
          (net->cachee->couche[j]) * (1 -
                                      net->cachee->couche[j]) * sum *
          (net->entree->couche[k]);
    }
}

is accessing memory passed the size of a float on the delta( net->cachee->delta[i] )
delta is a pointer to a float which is created in cCacheeCreer. is this correct? if so you are only creating a pointer to a single float. but i dont see why this would give a free error unless you then try and free past delta.
post back if i have missed something.

pierre-luc 11-13-2005 08:57 PM

Delta is a float pointer to a dynamic array created inside main function within cCacheeCreer or cSortieCreer.

Code:

  cCachee.delta = cCacheeCreer (&cCachee);
thanks for your feedback

dmail 11-13-2005 09:04 PM

so it is, im so used to using c++ i keep forgeting c style memory .oops


All times are GMT -5. The time now is 08:22 PM.