We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 150 - memory leak in SDL_thread.c
Summary: memory leak in SDL_thread.c
Status: RESOLVED FIXED
Alias: None
Product: SDL
Classification: Unclassified
Component: thread (show other bugs)
Version: 1.2.9
Hardware: x86 Windows (All)
: P2 normal
Assignee: Ryan C. Gordon
QA Contact: Sam Lantinga
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2006-02-22 09:04 UTC by Danijel Kopcinovic
Modified: 2006-03-11 20:18 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Danijel Kopcinovic 2006-02-22 09:04:17 UTC
In routine SDL_DelThread, I got a memory leak. It was due to not freeing thread lock if there were no more threads. Here is my fix for this:
static void SDL_DelThread(SDL_Thread *thread)
{
	int i;

	if ( thread_lock ) {
		SDL_mutexP(thread_lock);
		for ( i=0; i<SDL_numthreads; ++i ) {
			if ( thread == SDL_Threads[i] ) {
				break;
			}
		}
		if ( i < SDL_numthreads ) {
			if ( --SDL_numthreads > 0 ) {
				while ( i < SDL_numthreads ) {
					SDL_Threads[i] = SDL_Threads[i+1];
					++i;
				}
			} else {
				SDL_maxthreads = 0;
				free(SDL_Threads);
				SDL_Threads = NULL;
			}
#ifdef DEBUG_THREADS
			printf("Deleting thread (%d left - %d max)\n",
					SDL_numthreads, SDL_maxthreads);
#endif
		}
		SDL_mutexV(thread_lock);

    //  Added by Danijel Kopcinovic as a fix for memory leak (thread_lock was
    //  not deleted if there were no more threads).
    if (SDL_Threads == NULL) SDL_ThreadsQuit();
	}
}
Comment 1 Dmitry Yakimov 2006-02-28 00:01:24 UTC
Shall we did it in atexit?
Comment 2 Sam Lantinga 2006-03-09 10:34:39 UTC
(In reply to comment #1)
> Shall we did it in atexit?
> 

No, there are lots of issues with atexit:
* There may not be any C runtime
* The C runtime of the SDL library may be different from that of the application
* SDL may be dynamically loaded and then unloaded which would cause a crash at exit
* There's no guarantee that threads won't persist past our atexit call and be cleaned up in a subsequent atexit handler.
Comment 3 Sam Lantinga 2006-03-11 20:18:31 UTC
This is fixed in CVS, thanks!