Don't crash if freeing a hardware surface after the video mode has been
authorSam Lantinga <slouken@lokigames.com>
Thu, 12 Jul 2001 23:34:44 +0000
changeset 102 9162d62280b5
parent 101 825b2fa28e2e
child 103 e599cc4485fa
Don't crash if freeing a hardware surface after the video mode has been reset. Fortunately we keep track of the video memory internally.
src/video/dga/SDL_dgavideo.c
--- a/src/video/dga/SDL_dgavideo.c	Thu Jul 12 20:42:22 2001 +0000
+++ b/src/video/dga/SDL_dgavideo.c	Thu Jul 12 23:34:44 2001 +0000
@@ -740,42 +740,44 @@
 	vidmem_bucket *bucket, *freeable;
 
 	/* Look for the bucket in the current list */
-	bucket = (vidmem_bucket *)surface->hwdata;
-	if ( (bucket == NULL) || ! bucket->used ) {
-		return;
+	for ( bucket=&surfaces; bucket; bucket=bucket->next ) {
+		if ( bucket == (vidmem_bucket *)surface->hwdata ) {
+			break;
+		}
 	}
-
-	/* Add the memory back to the total */
+	if ( bucket && bucket->used ) {
+		/* Add the memory back to the total */
 #ifdef DGA_DEBUG
 	printf("Freeing bucket of %d bytes\n", bucket->size);
 #endif
-	surfaces_memleft += bucket->size;
+		surfaces_memleft += bucket->size;
 
-	/* Can we merge the space with surrounding buckets? */
-	bucket->used = 0;
-	if ( bucket->next && ! bucket->next->used ) {
+		/* Can we merge the space with surrounding buckets? */
+		bucket->used = 0;
+		if ( bucket->next && ! bucket->next->used ) {
 #ifdef DGA_DEBUG
 	printf("Merging with next bucket, for %d total bytes\n", bucket->size+bucket->next->size);
 #endif
-		freeable = bucket->next;
-		bucket->size += bucket->next->size;
-		bucket->next = bucket->next->next;
-		if ( bucket->next ) {
-			bucket->next->prev = bucket;
+			freeable = bucket->next;
+			bucket->size += bucket->next->size;
+			bucket->next = bucket->next->next;
+			if ( bucket->next ) {
+				bucket->next->prev = bucket;
+			}
+			free(freeable);
 		}
-		free(freeable);
-	}
-	if ( bucket->prev && ! bucket->prev->used ) {
+		if ( bucket->prev && ! bucket->prev->used ) {
 #ifdef DGA_DEBUG
 	printf("Merging with previous bucket, for %d total bytes\n", bucket->prev->size+bucket->size);
 #endif
-		freeable = bucket;
-		bucket->prev->size += bucket->size;
-		bucket->prev->next = bucket->next;
-		if ( bucket->next ) {
-			bucket->next->prev = bucket->prev;
+			freeable = bucket;
+			bucket->prev->size += bucket->size;
+			bucket->prev->next = bucket->next;
+			if ( bucket->next ) {
+				bucket->next->prev = bucket->prev;
+			}
+			free(freeable);
 		}
-		free(freeable);
 	}
 	surface->pixels = NULL;
 	surface->hwdata = NULL;