c - Pebble crashes on `realloc`, but only in a certain function -


i'm using custom vector in pebble app. pebble crashing on call realloc.

main.c

#include <pebble.h> #include "movement.h"  static pointarray point_array;  int main(void) {;     point_array_create(&point_array, 1);     gpoint point1 = (gpoint){.x = 1, .y = 1};     gpoint point2 = (gpoint){.x = 2, .y = 2};     gpoint point3 = (gpoint){.x = 3, .y = 3};      point_array_push(&point_array, point1);     point_array_push(&point_array, point2);     point_array_push(&point_array, point3);     app_log(app_log_level_debug, "done\n"); } 

movement.c

#include "movement.h" #include "pebble.h"   static void point_array_resize(pointarray *point_array){   point_array->capacity *= 2;   size_t new_size = point_array->capacity * sizeof(gpoint) + sizeof(gpoint);   point_array->points = (gpoint*)realloc(point_array->points, new_size); }   void point_array_create(pointarray *arr, int capacity) {     arr->points = (gpoint*)malloc(capacity * sizeof(gpoint));     arr->length = 0;     arr->capacity = capacity; }  void point_array_push(pointarray *point_array, gpoint point) {    app_log(app_log_level_debug, "pushing");    if (point_array->length > point_array->capacity) {     app_log(app_log_level_debug, "resizing");     point_array_resize(point_array);     app_log(app_log_level_debug, "successful resize");   }   point_array->points[point_array->length] = point;   point_array->length++;   app_log(app_log_level_debug, "+ length"); } 

movement.h

#include <pebble.h> #include <stdlib.h> #include <math.h>  typedef struct {   gpoint *points;   int length;   int capacity; } pointarray;  void point_array_create(pointarray *arr, int capacity);  void point_array_push(pointarray *point_array, gpoint point);  void point_array_destroy(pointarray *point_array, gpoint point);  gpoint move(gpoint point, float distance, float degrees); 

the logs show app crashing @ call realloc:

[debug] movement.c:20: pushing [debug] movement.c:29: + length [debug] movement.c:20: pushing [debug] movement.c:29: + length [debug] movement.c:20: pushing [debug] movement.c:23: resizing 

here's tried:

  • the code runs fine on both gcc , clang (!).
  • i verified point_array , point_array->points not null and
  • new_size larger existing size of point_array->points.
  • i looked @ this issue, doesn't seem apply.
  • i tried calling realloc @ bottom of point_array_create, , works fine. doesn't work in point_array_resize.

in point_array_push, test if need resize points @ top, test wrong. resize if length has exceeded capacity, means have overrun array.

instead, check see if length has reached capacity.

  if (point_array->length == point_array->capacity) {     app_log(app_log_level_debug, "resizing");     point_array_resize(point_array);     app_log(app_log_level_debug, "successful resize");   } 

Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -