Freezing why?

4 posts / 0 new
Last post
JosDuchIt
JosDuchIt's picture
Offline
Last seen: 8 years 1 month ago
Joined: 2012-01-08 20:57
Freezing why?

The code below compiles with no warnigns. Under gdb , gdb tells me it exited correctly, still it freezes my SAM440ep OS4.1.4 systematically.
???

  1. #define __USE_INLINE__
  2. #include <exec/types.h>
  3. #include <proto/exec.h>
  4. #include <exec/exec.h>
  5. #include <dos/dos.h>
  6. #include <proto/dos.h>
  7. #include <proto/intuition.h>
  8. #define Db(x) ; ///(x)
  9.  
  10.  
  11. struct GCmain
  12. {
  13. char *membuff[5]; // 5 main memory buffers of buffsize length - use them..
  14. }*gd;
  15.  
  16.  
  17. int main(void) {
  18. int c;
  19. struct Remember *rkey=NULL;
  20. if (!(gd = (struct GCmain *)AllocRemember (&rkey, sizeof(struct GCmain), MEMF_CLEAR))) ////
  21. { PutStr("no gcmain obtained\n");
  22. return 1;
  23. }
  24. for (c=0; c<5; ++c)
  25. { gd->membuff[c]=NULL;
  26. }
  27. if (!(getbuffers()))
  28. { PutStr("no buff obtained\n");
  29. return 1;
  30. }
  31. FreeRemember(&rkey,TRUE);
  32. // free all main buffers
  33. for (c=0; c<5; ++c)
  34. { if (gd->membuff[c]) FreeVec (gd->membuff[c]);
  35. }
  36.  
  37. }
  38.  
  39.  
  40. int getbuffers(void)
  41. {
  42. char *pt;
  43. int c;
  44.  
  45.  
  46. for (c=0; c<5; ++c)
  47. { if (!(pt = (char *)AllocVecTags (1024, AVT_Type, MEMF_SHARED, AVT_ClearWithValue, 0, TAG_END)))
  48. { PutStr("buffer alloc error\n");
  49. return (0);
  50. }
  51. gd->membuff[c] = pt;
  52. }
  53.  
  54.  
  55. return (1);
  56. }
Rigo
Rigo's picture
Offline
Last seen: 2 years 6 months ago
Joined: 2011-01-24 21:55
I would review your compiler

I would review your compiler switches, as this should give at least one warning about main() not returning any value (control reaches end of non-void function).

Try "-Wall -Werror -Wwrite-strings" for starters.

Simon

salass00
salass00's picture
Offline
Last seen: 7 months 1 week ago
Joined: 2011-02-03 11:27
It crashes because you access

It crashes because you access the AllocRemember() allocated memory after it's been freed:

  1. FreeRemember(&rkey,TRUE);
  2. for (c=0; c<5; ++c)
  3. { if (gd->membuff[c]) FreeVec (gd->membuff[c]);
  4. }

You need to change the order of the above to:

  1. for (c=0; c<5; ++c)
  2. { if (gd->membuff[c]) FreeVec (gd->membuff[c]);
  3. }
  4. FreeRemember(&rkey,TRUE);
JosDuchIt
JosDuchIt's picture
Offline
Last seen: 8 years 1 month ago
Joined: 2012-01-08 20:57
@salass00 so logic. Thanks a

@salass00
so logic. Thanks a lot
@simon: i'll remember that one too

Log in or register to post comments