[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [13875] trunk/blender/source/blender/ blenkernel/intern/cloth.c: Cloth: make cloth more crash resistant if memory allocation failed

Daniel Genrich daniel.genrich at gmx.net
Tue Feb 26 15:25:29 CET 2008


Revision: 13875
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=13875
Author:   genscher
Date:     2008-02-26 15:25:29 +0100 (Tue, 26 Feb 2008)

Log Message:
-----------
Cloth: make cloth more crash resistant if memory allocation failed

Modified Paths:
--------------
    trunk/blender/source/blender/blenkernel/intern/cloth.c

Modified: trunk/blender/source/blender/blenkernel/intern/cloth.c
===================================================================
--- trunk/blender/source/blender/blenkernel/intern/cloth.c	2008-02-26 14:00:13 UTC (rev 13874)
+++ trunk/blender/source/blender/blenkernel/intern/cloth.c	2008-02-26 14:25:29 UTC (rev 13875)
@@ -234,6 +234,20 @@
 	bvh->current_x = MEM_callocN ( sizeof ( MVert ) * bvh->numverts, "bvh->current_x" );
 	bvh->current_xold = MEM_callocN ( sizeof ( MVert ) * bvh->numverts, "bvh->current_xold" );
 	
+	if (bvh->current_x == NULL) 
+	{
+		printf("bvh: Out of memory.\n");
+		MEM_freeN(bvh);
+		return NULL;
+	}
+	
+	if (bvh->current_xold == NULL) 
+	{
+		printf("bvh: Out of memory.\n");
+		MEM_freeN(bvh);
+		return NULL;
+	}
+	
 	for(i = 0; i < bvh->numverts; i++)
 	{
 		VECCOPY(bvh->current_x[i].co, verts[i].tx);
@@ -1170,6 +1184,9 @@
 		
 		spring = ( ClothSpring * ) MEM_callocN ( sizeof ( ClothSpring ), "cloth spring" );
 		
+		if(!spring)
+			return 0;
+		
 		spring->ij = indexA;
 		spring->kl = indexB;
 		spring->restlen =  restlength;
@@ -1186,6 +1203,39 @@
 	return 0;
 }
 
+void cloth_free_errorsprings(Cloth *cloth, EdgeHash *edgehash, LinkNode **edgelist)
+{
+	unsigned int i = 0;
+	
+	if ( cloth->springs != NULL )
+	{
+		LinkNode *search = cloth->springs;
+		while(search)
+		{
+			ClothSpring *spring = search->link;
+						
+			MEM_freeN ( spring );
+			search = search->next;
+		}
+		BLI_linklist_free(cloth->springs, NULL);
+		
+		cloth->springs = NULL;
+	}
+	
+	if(edgelist)
+	{
+		for ( i = 0; i < cloth->numverts; i++ )
+		{
+			BLI_linklist_free ( edgelist[i],NULL );
+		}
+
+		MEM_freeN ( edgelist );
+	}
+	
+	if(cloth->edgehash)
+		BLI_edgehash_free ( cloth->edgehash, NULL );
+}
+
 int cloth_build_springs ( ClothModifierData *clmd, DerivedMesh *dm )
 {
 	Cloth *cloth = clmd->clothObject;
@@ -1210,6 +1260,10 @@
 	cloth->springs = NULL;
 
 	edgelist = MEM_callocN ( sizeof ( LinkNode * ) * numverts, "cloth_edgelist_alloc" );
+	
+	if(!edgelist)
+		return 0;
+	
 	for ( i = 0; i < numverts; i++ )
 	{
 		edgelist[i] = NULL;
@@ -1244,9 +1298,15 @@
 			
 			BLI_linklist_prepend ( &cloth->springs, spring );
 		}
+		else
+		{
+			cloth_free_errorsprings(cloth, edgehash, edgelist);
+			return 0;
+		}
 	}
 	
-	clmd->sim_parms->avg_spring_len /= struct_springs;
+	if(struct_springs > 0)
+		clmd->sim_parms->avg_spring_len /= struct_springs;
 	
 	for(i = 0; i < numverts; i++)
 	{
@@ -1257,6 +1317,12 @@
 	for ( i = 0; i < numfaces; i++ )
 	{
 		spring = ( ClothSpring *) MEM_callocN ( sizeof ( ClothSpring ), "cloth spring" );
+		
+		if(!spring)
+		{
+			cloth_free_errorsprings(cloth, edgehash, edgelist);
+			return 0;
+		}
 
 		spring->ij = MIN2(mface[i].v1, mface[i].v3);
 		spring->kl = MAX2(mface[i].v3, mface[i].v1);
@@ -1274,6 +1340,12 @@
 		if ( mface[i].v4 )
 		{
 			spring = ( ClothSpring * ) MEM_callocN ( sizeof ( ClothSpring ), "cloth spring" );
+			
+			if(!spring)
+			{
+				cloth_free_errorsprings(cloth, edgehash, edgelist);
+				return 0;
+			}
 
 			spring->ij = MIN2(mface[i].v2, mface[i].v4);
 			spring->kl = MAX2(mface[i].v4, mface[i].v2);
@@ -1311,6 +1383,12 @@
 						   && ( index2!=tspring2->ij ) )
 			{
 				spring = ( ClothSpring * ) MEM_callocN ( sizeof ( ClothSpring ), "cloth spring" );
+				
+				if(!spring)
+				{
+					cloth_free_errorsprings(cloth, edgehash, edgelist);
+					return 0;
+				}
 
 				spring->ij = MIN2(tspring2->ij, index2);
 				spring->kl = MAX2(tspring2->ij, index2);
@@ -1330,12 +1408,15 @@
 	
 	cloth->numsprings = struct_springs + shear_springs + bend_springs;
 	
-	for ( i = 0; i < numverts; i++ )
+	if ( edgelist )
 	{
-		BLI_linklist_free ( edgelist[i],NULL );
+		for ( i = 0; i < numverts; i++ )
+		{
+			BLI_linklist_free ( edgelist[i],NULL );
+		}
+	
+		MEM_freeN ( edgelist );
 	}
-	if ( edgelist )
-		MEM_freeN ( edgelist );
 	
 	cloth->edgehash = edgehash;
 	





More information about the Bf-blender-cvs mailing list