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 1029162d62280b5
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
     1.1 --- a/src/video/dga/SDL_dgavideo.c	Thu Jul 12 20:42:22 2001 +0000
     1.2 +++ b/src/video/dga/SDL_dgavideo.c	Thu Jul 12 23:34:44 2001 +0000
     1.3 @@ -740,42 +740,44 @@
     1.4  	vidmem_bucket *bucket, *freeable;
     1.5  
     1.6  	/* Look for the bucket in the current list */
     1.7 -	bucket = (vidmem_bucket *)surface->hwdata;
     1.8 -	if ( (bucket == NULL) || ! bucket->used ) {
     1.9 -		return;
    1.10 +	for ( bucket=&surfaces; bucket; bucket=bucket->next ) {
    1.11 +		if ( bucket == (vidmem_bucket *)surface->hwdata ) {
    1.12 +			break;
    1.13 +		}
    1.14  	}
    1.15 -
    1.16 -	/* Add the memory back to the total */
    1.17 +	if ( bucket && bucket->used ) {
    1.18 +		/* Add the memory back to the total */
    1.19  #ifdef DGA_DEBUG
    1.20  	printf("Freeing bucket of %d bytes\n", bucket->size);
    1.21  #endif
    1.22 -	surfaces_memleft += bucket->size;
    1.23 +		surfaces_memleft += bucket->size;
    1.24  
    1.25 -	/* Can we merge the space with surrounding buckets? */
    1.26 -	bucket->used = 0;
    1.27 -	if ( bucket->next && ! bucket->next->used ) {
    1.28 +		/* Can we merge the space with surrounding buckets? */
    1.29 +		bucket->used = 0;
    1.30 +		if ( bucket->next && ! bucket->next->used ) {
    1.31  #ifdef DGA_DEBUG
    1.32  	printf("Merging with next bucket, for %d total bytes\n", bucket->size+bucket->next->size);
    1.33  #endif
    1.34 -		freeable = bucket->next;
    1.35 -		bucket->size += bucket->next->size;
    1.36 -		bucket->next = bucket->next->next;
    1.37 -		if ( bucket->next ) {
    1.38 -			bucket->next->prev = bucket;
    1.39 +			freeable = bucket->next;
    1.40 +			bucket->size += bucket->next->size;
    1.41 +			bucket->next = bucket->next->next;
    1.42 +			if ( bucket->next ) {
    1.43 +				bucket->next->prev = bucket;
    1.44 +			}
    1.45 +			free(freeable);
    1.46  		}
    1.47 -		free(freeable);
    1.48 -	}
    1.49 -	if ( bucket->prev && ! bucket->prev->used ) {
    1.50 +		if ( bucket->prev && ! bucket->prev->used ) {
    1.51  #ifdef DGA_DEBUG
    1.52  	printf("Merging with previous bucket, for %d total bytes\n", bucket->prev->size+bucket->size);
    1.53  #endif
    1.54 -		freeable = bucket;
    1.55 -		bucket->prev->size += bucket->size;
    1.56 -		bucket->prev->next = bucket->next;
    1.57 -		if ( bucket->next ) {
    1.58 -			bucket->next->prev = bucket->prev;
    1.59 +			freeable = bucket;
    1.60 +			bucket->prev->size += bucket->size;
    1.61 +			bucket->prev->next = bucket->next;
    1.62 +			if ( bucket->next ) {
    1.63 +				bucket->next->prev = bucket->prev;
    1.64 +			}
    1.65 +			free(freeable);
    1.66  		}
    1.67 -		free(freeable);
    1.68  	}
    1.69  	surface->pixels = NULL;
    1.70  	surface->hwdata = NULL;