fix memory alloc/free bugs, and one in render_threads where the last row was left out
authorTero Marttila <terom@fixme.fi>
Tue, 17 Jun 2008 18:15:43 +0300
changeset 21 e2916f8ebaa6
parent 20 7512207c9041
child 22 4627760fc0d1
fix memory alloc/free bugs, and one in render_threads where the last row was left out

committer: Tero Marttila <terom@fixme.fi>
node_main.c
render.c
render.h
render_threads.c
--- a/node_main.c	Tue Jun 17 18:05:08 2008 +0300
+++ b/node_main.c	Tue Jun 17 18:15:43 2008 +0300
@@ -65,12 +65,13 @@
 
 void handle_client (int sock) {
     double duration;
-    struct render *ctx;
+    struct render *ctx = NULL;
     FILE *fh;
     u_int8_t mode;
     u_int32_t img_w, img_h;
     double x1, y1, x2, y2;
 
+    struct render_threads *threads_info = NULL;
     
     // open it as a FILE*
     if (!(fh = fdopen(sock, "r+")))
@@ -107,7 +108,6 @@
     if (render_io_stream(ctx, fh))
         ERROR("render_io_stream");
     
-    struct render_threads *threads_info = NULL;
 
     // render threaded \o/
     if (!(threads_info = render_threads_alloc(ctx)))
@@ -128,7 +128,9 @@
     // fall through to just clean up normally
 
 error:
-    // free the threads_info?
+    if (ctx)
+        render_free(ctx);
+
     if (threads_info)
         render_threads_free(threads_info);
 
--- a/render.c	Tue Jun 17 18:05:08 2008 +0300
+++ b/render.c	Tue Jun 17 18:15:43 2008 +0300
@@ -19,6 +19,10 @@
     return ctx;
 }
 
+void render_free(struct render *ctx) {
+    free(ctx);
+}
+
 int render_set_mode (struct render *ctx, int mode) {
     if (mode != RENDER_RAW && mode != RENDER_PNG)
         return -1;
--- a/render.h	Tue Jun 17 18:05:08 2008 +0300
+++ b/render.h	Tue Jun 17 18:15:43 2008 +0300
@@ -31,6 +31,7 @@
  * Alloc a new render context
  */
 struct render *render_alloc ();
+void render_free(struct render *ctx);
 
 /*
  * Clear out the value of the given render context
--- a/render_threads.c	Tue Jun 17 18:05:08 2008 +0300
+++ b/render_threads.c	Tue Jun 17 18:15:43 2008 +0300
@@ -389,8 +389,8 @@
             pthread_mutex_lock(&ctx->process_row_mut);
         }
 
-        // are all the slices done now?
-        if (ctx->slices_running == 0) {
+        // are all the rows and slices done now?
+        if (ctx->slices_running == 0 && ctx->waiting_rows == 0) {
             // unlock the mutex so the worker threads don't get stuck on it
             pthread_mutex_unlock(&ctx->process_row_mut);
 
@@ -429,13 +429,13 @@
     for (i = 0; i < ctx->slice_count; i++) {
         if (pthread_cancel(ctx->threads[i].thread))
             PWARNING("pthread_cancel(worker:%d)", i);
-        else {
-            if (pthread_join(ctx->threads[i].thread, &retval)) {
-                PWARNING("pthread_join(worker:%d)", i);
-            } else {
-                if (retval != PTHREAD_CANCELED)
-                    PWARNING("worker:%d retval = %p", i, retval);
-            }
+
+        // join it in any case
+        if (pthread_join(ctx->threads[i].thread, &retval)) {
+            PWARNING("pthread_join(worker:%d)", i);
+        } else {
+            if (retval != PTHREAD_CANCELED)
+                PWARNING("worker:%d retval = %p", i, retval);
         }
     }
     
@@ -509,6 +509,9 @@
     if (!(ctx = calloc(1, sizeof(*ctx))))
         ERROR("calloc");
     
+    // flag it for free()ing
+    ctx->owned_by_me = 1;
+    
     // init with silent fall-through
     if (render_threads_init(ctx, render_info))
         goto error;