[cig-commits] commit: Fix memory errors

Mercurial hg at geodynamics.org
Sat Apr 7 15:10:26 PDT 2012


changeset:   448:30411e58fae6
tag:         tip
user:        Walter Landry <wlandry at caltech.edu>
date:        Sat Apr 07 15:10:18 2012 -0700
files:       Weights/src/DVCWeights.cxx
description:
Fix memory errors


diff -r 744dbfab0c93 -r 30411e58fae6 Weights/src/DVCWeights.cxx
--- a/Weights/src/DVCWeights.cxx	Thu Mar 15 12:30:16 2012 -0700
+++ b/Weights/src/DVCWeights.cxx	Sat Apr 07 15:10:18 2012 -0700
@@ -274,7 +274,7 @@ void _DVCWeights_ClaimCells(struct chain
         if(cells[cell_num0].p == -1){//if cell unowned then claim cell
             /* This is the bit needed for mallocing */
             /* do a test here to see if we need to realloc bchain->new_claimed_cells and bchain->new_bound_cells */
-            if( count > bchain->new_claimed_cells_malloced - 1 ){
+            if( count >= bchain->new_claimed_cells_malloced - 1 ){
                 temp = (int *)realloc( bchain->new_claimed_cells, (bchain->new_claimed_cells_malloced + DVC_INC)*sizeof(int) );
                 bchain->new_claimed_cells = temp;
                 bchain->new_claimed_cells_malloced += DVC_INC;
@@ -303,10 +303,23 @@ void _DVCWeights_ClaimCells(struct chain
         
                 dist1 = _DVCWeights_DistanceTest(x0,y0,z0,x1,y1,z1,x2,y2,z2);
                 if(dist1 > 0.0){
-                    bchain->new_claimed_cells[count] = cell_num0;
-                    bchain->numclaimed++;
-                    count++;
-                    cells[cell_num0].p = p_i;// this cell is now owned by particle p_i
+                  if( count >= bchain->new_claimed_cells_malloced - 1 ){
+                    temp = (int *)realloc( bchain->new_claimed_cells,
+                                           (bchain->new_claimed_cells_malloced
+                                            + DVC_INC)*sizeof(int) );
+                    bchain->new_claimed_cells = temp;
+                    bchain->new_claimed_cells_malloced += DVC_INC;
+                    temp = (int *)realloc( bchain->new_bound_cells,
+                                           (bchain->new_bound_cells_malloced
+                                            + DVC_INC)*sizeof(int) );
+                    bchain->new_bound_cells = temp;
+                    bchain->new_bound_cells_malloced += DVC_INC;      
+                  }
+                  bchain->new_claimed_cells[count] = cell_num0;
+                  bchain->numclaimed++;
+                  count++;
+                  cells[cell_num0].p = p_i;// this cell is now owned
+                                           // by particle p_i
                 }
             }//if
         }//else
@@ -332,7 +345,7 @@ void _DVCWeights_ClaimCells2D(struct cha
         if(cells[cell_num0].p == -1){//if cell unowned then claim cell
             /* This is the bit needed for mallocing */
             /* do a test here to see if we need to realloc bchain->new_claimed_cells and bchain->new_bound_cells */
-            if( count > bchain->new_claimed_cells_malloced - 1 ){
+            if( count >= bchain->new_claimed_cells_malloced - 1 ){
                 temp = (int *)realloc( bchain->new_claimed_cells, (bchain->new_claimed_cells_malloced + DVC_INC)*sizeof(int) );
                 bchain->new_claimed_cells = temp;
                 bchain->new_claimed_cells_malloced += DVC_INC;
@@ -358,10 +371,23 @@ void _DVCWeights_ClaimCells2D(struct cha
         
                 dist1 = _DVCWeights_DistanceTest2D(x0,y0,x1,y1,x2,y2);
                 if(dist1 > 0.0){
+                  if( count >= bchain->new_claimed_cells_malloced - 1 ){
+                    temp = (int *)realloc( bchain->new_claimed_cells,
+                                           (bchain->new_claimed_cells_malloced
+                                            + DVC_INC)*sizeof(int) );
+                    bchain->new_claimed_cells = temp;
+                    bchain->new_claimed_cells_malloced += DVC_INC;
+                    temp = (int *)realloc( bchain->new_bound_cells,
+                                           (bchain->new_bound_cells_malloced
+                                            + DVC_INC)*sizeof(int) );
+                    bchain->new_bound_cells = temp;
+                    bchain->new_bound_cells_malloced += DVC_INC;      
+                  }
                     bchain->new_claimed_cells[count] = cell_num0;
                     bchain->numclaimed++;
                     count++;
-                    cells[cell_num0].p = p_i;// this cell is now owned by particle p_i
+                    cells[cell_num0].p = p_i;// this cell is now owned
+                                             // by particle p_i
                 }
             }//if
         }//else
@@ -419,7 +445,7 @@ void _DVCWeights_UpdateBchain(struct cha
                 if(cells[cell_num1].p != p_i && cells[cell_num1].done != 1){
                     /* This is the bit needed for mallocing */     
                     /* do a test here to see if we need to realloc bchain->new_claimed_cells and bchain->new_bound_cells */
-                    if( count > bchain->new_bound_cells_malloced - 1 ){
+                    if( count >= bchain->new_bound_cells_malloced - 1 ){
                         temp = (int *)realloc( bchain->new_claimed_cells, (bchain->new_claimed_cells_malloced + DVC_INC)*sizeof(int) );
                         bchain->new_claimed_cells = temp;
                         bchain->new_claimed_cells_malloced += DVC_INC;
@@ -470,7 +496,7 @@ void _DVCWeights_UpdateBchain2D(struct c
                 if(cells[cell_num1].p != p_i && cells[cell_num1].done != 1){
                     /* This is the bit needed for mallocing */     
                     /* do a test here to see if we need to realloc bchain->new_claimed_cells and bchain->new_bound_cells */
-                    if( count > bchain->new_bound_cells_malloced - 1 ){
+                    if( count >= bchain->new_bound_cells_malloced - 1 ){
                         temp = (int *)realloc( bchain->new_claimed_cells, (bchain->new_claimed_cells_malloced + DVC_INC)*sizeof(int) );
                         bchain->new_claimed_cells = temp;
                         bchain->new_claimed_cells_malloced += DVC_INC;



More information about the CIG-COMMITS mailing list