Revision: 75769
          http://sourceforge.net/p/brlcad/code/75769
Author:   d_rossberg
Date:     2020-05-12 15:30:31 +0000 (Tue, 12 May 2020)
Log Message:
-----------
fixed memory leak

Modified Paths:
--------------
    rt^3/trunk/src/coreInterface/BagOfTriangles.cpp

Modified: rt^3/trunk/src/coreInterface/BagOfTriangles.cpp
===================================================================
--- rt^3/trunk/src/coreInterface/BagOfTriangles.cpp     2020-05-12 15:14:38 UTC 
(rev 75768)
+++ rt^3/trunk/src/coreInterface/BagOfTriangles.cpp     2020-05-12 15:30:31 UTC 
(rev 75769)
@@ -238,7 +238,7 @@
 }
 
 
-static void FreeBotInternal
+static void CleanBotInternal
 (
     rt_bot_internal* bot
 ) {
@@ -245,86 +245,118 @@
     assert(bot != 0);
 
     RT_BOT_CK_MAGIC(bot);
-    bot->magic = 0; /* sanity */
 
     if (bot->tie != 0)
         bot->tie = 0;
 
     if (bot->vertices != 0) {
-        bu_free(bot->vertices, "bot interface FreeBotInternal(): vertices");
+        bu_free(bot->vertices, "bot interface CleanBotInternal(): vertices");
         bot->vertices     = 0;
         bot->num_vertices = 0;
     }
 
     if (bot->faces != 0) {
-        bu_free(bot->faces, "bot interface FreeBotInternal(): faces");
+        bu_free(bot->faces, "bot interface CleanBotInternal(): faces");
         bot->faces     = 0;
         bot->num_faces = 0;
     }
 
     if (bot->thickness != 0) {
-        bu_free(bot->thickness, "bot interface FreeBotInternal(): thickness");
+        bu_free(bot->thickness, "bot interface CleanBotInternal(): thickness");
         bot->thickness = 0;
     }
 
     if (bot->face_mode != 0) {
-        bu_free(bot->face_mode, "bot interface FreeBotInternal(): face_mode");
+        bu_free(bot->face_mode, "bot interface CleanBotInternal(): face_mode");
         bot->face_mode = 0;
     }
 
-    if (bot->normals != 0)
-        bu_free(bot->normals, "bot interface FreeBotInternal(): normals");
+    if (bot->normals != 0) {
+        bu_free(bot->normals, "bot interface CleanBotInternal(): normals");
+        bot->normals     = 0;
+        bot->num_normals = 0;
+    }
 
-    if (bot->face_normals != 0)
-        bu_free(bot->face_normals, "bot interface FreeBotInternal(): 
face_normals");
+    if (bot->face_normals != 0) {
+        bu_free(bot->face_normals, "bot interface CleanBotInternal(): 
face_normals");
+        bot->face_normals     = 0;
+        bot->num_face_normals = 0;
+    }
+}
 
+
+static void FreeBotInternal
+(
+    rt_bot_internal* bot
+) {
+    assert(bot != 0);
+
+    RT_BOT_CK_MAGIC(bot);
+
+    CleanBotInternal(bot);
+    bot->magic = 0; /* sanity */
     bu_free(bot, "bot interface FreeBotInternal(): rt_bot_internal");
 }
 
 
-rt_bot_internal* CloneBotInternal
+static void CopyBotInternal
 (
-    const rt_bot_internal& bot
+    rt_bot_internal*       copy,
+    const rt_bot_internal* original
 ) {
-    RT_BOT_CK_MAGIC(&bot);
+    RT_BOT_CK_MAGIC(copy);
+    RT_BOT_CK_MAGIC(original);
 
-    struct rt_bot_internal* ret;
-    BU_ALLOC(ret, struct rt_bot_internal);
-    *ret = bot;
+    CleanBotInternal(copy);
+    *copy = *original;
 
-    if (bot.faces != 0) {
-        ret->faces =  static_cast<int*>(bu_malloc(3 * bot.num_faces * 
sizeof(int), "bot interface CloneBotInternal(): faces"));
+    if (original->faces != 0) {
+        copy->faces =  static_cast<int*>(bu_malloc(3 * original->num_faces * 
sizeof(int), "bot interface CopyBotInternal(): faces"));
 
-        memcpy(ret->faces, bot.faces, 3 * bot.num_faces * sizeof(int));
+        memcpy(copy->faces, original->faces, 3 * original->num_faces * 
sizeof(int));
     }
 
-    if (bot.vertices != 0) {
-        ret->vertices = static_cast<fastf_t*>(bu_malloc(3 * bot.num_vertices * 
sizeof(fastf_t), "bot interface CloneBotInternal(): vertices"));
+    if (original->vertices != 0) {
+        copy->vertices = static_cast<fastf_t*>(bu_malloc(3 * 
original->num_vertices * sizeof(fastf_t), "bot interface CopyBotInternal(): 
vertices"));
 
-        memcpy(ret->vertices, bot.vertices, 3 * bot.num_vertices * 
sizeof(fastf_t));
+        memcpy(copy->vertices, original->vertices, 3 * original->num_vertices 
* sizeof(fastf_t));
     }
 
-    if (bot.thickness != 0) {
-        ret->thickness = static_cast<fastf_t*>(bu_malloc(bot.num_faces * 
sizeof(fastf_t), "bot interface CloneBotInternal(): thickness"));
+    if (original->thickness != 0) {
+        copy->thickness = static_cast<fastf_t*>(bu_malloc(original->num_faces 
* sizeof(fastf_t), "bot interface CopyBotInternal(): thickness"));
 
-        memcpy(ret->thickness, bot.thickness, bot.num_faces * sizeof(fastf_t));
+        memcpy(copy->thickness, original->thickness, original->num_faces * 
sizeof(fastf_t));
     }
 
-    if (bot.face_mode != 0)
-        ret->face_mode = bu_bitv_dup(bot.face_mode);
+    if (original->face_mode != 0)
+        copy->face_mode = bu_bitv_dup(original->face_mode);
 
-    if (bot.normals != 0) {
-        ret->normals = static_cast<fastf_t*>(bu_malloc(3 * bot.num_normals * 
sizeof(fastf_t), "bot interface CloneBotInternal(): normals"));
+    if (original->normals != 0) {
+        copy->normals = static_cast<fastf_t*>(bu_malloc(3 * 
original->num_normals * sizeof(fastf_t), "bot interface CopyBotInternal(): 
normals"));
 
-        memcpy(ret->normals, bot.normals, 3 * bot.num_normals * 
sizeof(fastf_t));
+        memcpy(copy->normals, original->normals, 3 * original->num_normals * 
sizeof(fastf_t));
     }
 
-    if (bot.face_normals != 0) {
-        ret->face_normals = static_cast<int*>(bu_malloc(3 * 
bot.num_face_normals * sizeof(int), "bot interface CloneBotInternal(): 
face_normals"));
+    if (original->face_normals != 0) {
+        copy->face_normals = static_cast<int*>(bu_malloc(3 * 
original->num_face_normals * sizeof(int), "bot interface CopyBotInternal(): 
face_normals"));
 
-        memcpy(ret->face_normals, bot.face_normals, 3 * bot.num_face_normals * 
sizeof(int));
+        memcpy(copy->face_normals, original->face_normals, 3 * 
original->num_face_normals * sizeof(int));
     }
+}
 
+
+rt_bot_internal* CloneBotInternal
+(
+    const rt_bot_internal& bot
+) {
+    RT_BOT_CK_MAGIC(&bot);
+
+    struct rt_bot_internal* ret;
+
+    BU_GET(ret, rt_bot_internal);
+    ret->magic = RT_BOT_INTERNAL_MAGIC;
+    CopyBotInternal(ret, &bot);
+
     return ret;
 }
 
@@ -510,11 +542,10 @@
             rt_bot_internal*       thisInternal     = Internal();
             const rt_bot_internal* originalInternal = original.Internal();
 
-            thisInternal = CloneBotInternal(*originalInternal);
+            CopyBotInternal(thisInternal, originalInternal);
         }
-        else {
+        else
             BU_UNSETJUMP;
-        }
 
         BU_UNSETJUMP;
     }
@@ -896,9 +927,8 @@
 
         ret = Face(bot, bot->num_faces - 1);
     }
-    else {
+    else
         BU_UNSETJUMP;
-    }
 
     BU_UNSETJUMP;
 

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.



_______________________________________________
BRL-CAD Source Commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/brlcad-commits

Reply via email to