possible fix for some errors with vector, and fixed ARC_Text_SetString memory leak

This commit is contained in:
herbglitch 2023-08-26 01:36:22 +00:00
parent 1450bb7c86
commit bb67a87451
2 changed files with 11 additions and 1 deletions

View file

@ -32,6 +32,9 @@ void ARC_Text_SetString(ARC_Text *text, ARC_Renderer *renderer, ARC_String *stri
text->bounds.w = surface->w; text->bounds.w = surface->w;
text->bounds.h = surface->h; text->bounds.h = surface->h;
if(text->texture){
SDL_DestroyTexture(text->texture);
}
text->texture = SDL_CreateTextureFromSurface((SDL_Renderer *)renderer, surface); text->texture = SDL_CreateTextureFromSurface((SDL_Renderer *)renderer, surface);
SDL_FreeSurface(surface); SDL_FreeSurface(surface);

View file

@ -14,7 +14,7 @@ void ARC_Vector_Create(ARC_Vector **vector){
*vector = (ARC_Vector *) malloc(sizeof(ARC_Vector)); *vector = (ARC_Vector *) malloc(sizeof(ARC_Vector));
(*vector)->currentSize = (uint32_t *)malloc(sizeof(uint32_t)); (*vector)->currentSize = (uint32_t *)malloc(sizeof(uint32_t));
(*vector)->capacity = (uint32_t *)malloc(sizeof(uint32_t)); (*vector)->capacity = (uint32_t *)malloc(sizeof(uint32_t));
(*vector)->data = (void *)malloc(sizeof(void *)); (*vector)->data = (void **)malloc(sizeof(void *));
*(*vector)->currentSize = 0; *(*vector)->currentSize = 0;
*(*vector)->capacity = 1; *(*vector)->capacity = 1;
@ -77,6 +77,9 @@ void ARC_Vector_RemoveIndex(ARC_Vector *vector, uint32_t *index){
void ARC_Vector_RemoveIndexNoCheck(ARC_Vector *vector, uint32_t *index){ void ARC_Vector_RemoveIndexNoCheck(ARC_Vector *vector, uint32_t *index){
for(uint32_t i = *index; i <= *vector->currentSize; i++){ for(uint32_t i = *index; i <= *vector->currentSize; i++){
if(i + 1 >= *vector->currentSize - 1){
break;
}
vector->data[i] = vector->data[i + 1]; vector->data[i] = vector->data[i + 1];
} }
@ -87,6 +90,10 @@ void ARC_Vector_RemoveIndexNoCheck(ARC_Vector *vector, uint32_t *index){
} }
*vector->capacity >>= 1; *vector->capacity >>= 1;
if(*vector->capacity <= 0){
*vector->capacity = 1;
}
vector->data = (void *)realloc(vector->data, sizeof(void *) * *vector->capacity); vector->data = (void *)realloc(vector->data, sizeof(void *) * *vector->capacity);
} }