orangeduck / tgc

A Tiny Garbage Collector for C
Other
968 stars 64 forks source link

Tgc crashes when mallocing in a loop #13

Closed mgood7123 closed 6 years ago

mgood7123 commented 6 years ago
#include "tgc.c"

void tgc_start2(tgc_t *gc) {
  void * stk;
  gc->bottom = &stk;
  gc->paused = 0;
  gc->nitems = 0;
  gc->nslots = 0;
  gc->mitems = 0;
  gc->nfrees = 0;
  gc->maxptr = 0;
  gc->items = NULL;
  gc->frees = NULL;
  gc->minptr = UINTPTR_MAX;
  gc->loadfactor = 0.9;
  gc->sweepfactor = 0.5;
}
#include <stdio.h>
tgc_t Garbage_Collector_Program;

static void Garbage_Collector_Dump_Mem(FILE *fp) { /*return tgc_dump(&Garbage_Collector_Program, fp); */}
#define Garbage_Collector_Start() tgc_start2(&Garbage_Collector_Program)
#define Garbage_Collector_Cleanup() tgc_run(&Garbage_Collector_Program)
#define Garbage_Collector_Pause() tgc_pause(&Garbage_Collector_Program)
#define Garbage_Collector_Resume() tgc_resume(&Garbage_Collector_Program)
#define Garbage_Collector_Shutdown() tgc_stop(&Garbage_Collector_Program)

#define malloc(size)  tgc_alloc(&Garbage_Collector_Program, size)
#define calloc(num, size)  tgc_calloc(&Garbage_Collector_Program, num, size)
#define realloc(ptr, size) tgc_realloc(&Garbage_Collector_Program, ptr, size)
#define free(ptr)    tgc_free(&Garbage_Collector_Program, ptr)

int main()
{
    printf("\ntest 1: shutdown and start up\n");
    Garbage_Collector_Start();
    Garbage_Collector_Shutdown();
    Garbage_Collector_Start();
    Garbage_Collector_Shutdown();
    Garbage_Collector_Start();
    printf("\ntest 2: malloc an area of 1, 10 times and clean up\n");
    for (int i = 0, i < 1, i++) {
        malloc(1); // seems to cause the program to crash and abort the compilation, even if done indirectly
    }
    Garbage_Collector_Shutdown();
    Garbage_Collector_Start();
    //Garbage_Collector_Cleanup();
    /*
    printf("\ntest 3: clean up on segfault\n");
    printf("testing garbage collector and seg fault handler\n");
    char * a = malloc(1);
    a = realloc(a,2);
    Garbage_Collector_Dump_Mem(a);
    Garbage_Collector_Shutdown();
    */
    return 0;
}

when attempting to compile with Mobile C (IOS APP) it crashes but does not happen normally if the tgc is not included or if malloc is outside of the for loop

mgood7123 commented 6 years ago

I am unable to reproduce it on gnu linux (both -m32 and 64 bit)

mgood7123 commented 6 years ago

Tested with

#define Garbage_Collector_Start() tgc_start(&Garbage_Collector_Program, &argc)
int main(int argc)

and it still crashes

mgood7123 commented 6 years ago

nvm it was the for (, , ) that was causing it to crash