Index: libhb/hb.c =================================================================== --- libhb/hb.c (revision 1033) +++ libhb/hb.c (working copy) @@ -696,7 +696,7 @@ * @param h Handle to hb_handle_t. * @param job Handle to hb_job_t. */ -void hb_add( hb_handle_t * h, hb_job_t * job ) +const hb_job_t * hb_add( hb_handle_t * h, hb_job_t * job ) { hb_job_t * job_copy; hb_title_t * title, * title_copy; @@ -923,6 +923,10 @@ hb_list_add( h->jobs, job_copy ); h->job_count = hb_count(h); h->job_count_permanent++; + + hb_job_reference( job_copy ); + + return( job_copy ); } /** @@ -939,6 +943,7 @@ h->job_count_permanent--; /* XXX free everything XXX */ + hb_job_dereference( job ); } /** @@ -1207,3 +1212,27 @@ hb_unlock( h->state_lock ); hb_unlock( h->pause_lock ); } + +/* + * Register an interest in referencing this job, it won't be deleted + * until this party deregisters its interest in it. + */ +void hb_job_reference( const hb_job_t * job_copy ) +{ + hb_job_t * job = (hb_job_t * )job_copy; + + job->reference_count++; +} + +void hb_job_dereference( const hb_job_t * job_copy ) +{ + hb_job_t * job = (hb_job_t * )job_copy; + + job->reference_count--; + + if( job->reference_count <= 0 ) + { + hb_log( "Freeing job 0x%x", job ); + free( job ); + } +} Index: libhb/hb.h =================================================================== --- libhb/hb.h (revision 1033) +++ libhb/hb.h (working copy) @@ -84,7 +84,7 @@ int hb_count( hb_handle_t * ); hb_job_t * hb_job( hb_handle_t *, int ); hb_job_t * hb_current_job( hb_handle_t * h ); -void hb_add( hb_handle_t *, hb_job_t * ); +const hb_job_t * hb_add( hb_handle_t *, hb_job_t * ); void hb_rem( hb_handle_t *, hb_job_t * ); void hb_start( hb_handle_t * ); @@ -105,6 +105,9 @@ Aborts all current jobs if any, frees memory. */ void hb_close( hb_handle_t ** ); +void hb_job_reference( const hb_job_t * job_copy ); +void hb_job_dereference( const hb_job_t * job_copy ); + #ifdef __cplusplus } #endif Index: libhb/work.c =================================================================== --- libhb/work.c (revision 1033) +++ libhb/work.c (working copy) @@ -703,7 +703,7 @@ hb_buffer_pool_free(); hb_title_close( &job->title ); - free( job ); + hb_job_dereference( job ); } /** Index: libhb/common.h =================================================================== --- libhb/common.h (revision 1033) +++ libhb/common.h (working copy) @@ -105,6 +105,12 @@ *****************************************************************************/ struct hb_job_s { + /* + * Number of folks interested in this job, only free when equal to + * 0. + */ + int reference_count; + /* ID assigned by UI so it can groups job passes together */ int sequence_id; Index: macosx/Controller.mm =================================================================== --- macosx/Controller.mm (revision 1033) +++ macosx/Controller.mm (working copy) @@ -1557,6 +1586,7 @@ hb_list_t * list = hb_get_titles( fHandle ); hb_title_t * title = (hb_title_t *) hb_list_item( list, [fSrcTitlePopUp indexOfSelectedItem] ); hb_job_t * job = title->job; + const hb_job_t * addedJob; // job returned by hb_add // Assign a sequence number, starting at zero, to each job added so they can // be lumped together in the UI. @@ -1598,8 +1628,9 @@ * Add the pre-scan job */ job->sequence_id++; // for job grouping - hb_add( fHandle, job ); - + addedJob = hb_add( fHandle, job ); + hb_job_reference (addedJob); // keep a reference for the queue + job->x264opts = x264opts_tmp; } else @@ -1623,7 +1654,8 @@ job->pass = 1; job->sequence_id++; // for job grouping - hb_add( fHandle, job ); + addedJob = hb_add( fHandle, job ); + hb_job_reference (addedJob); // keep a reference for the queue job->pass = 2; job->sequence_id++; // for job grouping @@ -1633,14 +1665,16 @@ job->select_subtitle = subtitle_tmp; - hb_add( fHandle, job ); + addedJob = hb_add( fHandle, job ); + hb_job_reference (addedJob); // keep a reference for the queue } else { job->indepth_scan = 0; job->pass = 0; job->sequence_id++; // for job grouping - hb_add( fHandle, job ); + addedJob = hb_add( fHandle, job ); + hb_job_reference (addedJob); // keep a reference for the queue } NSString *destinationDirectory = [[fDstFile2Field stringValue] stringByDeletingLastPathComponent]; Index: macosx/HBQueueController.mm =================================================================== --- macosx/HBQueueController.mm (revision 1033) +++ macosx/HBQueueController.mm (working copy) @@ -145,8 +148,12 @@ { // Delete this job plus the following ones in the sequence hb_rem( h, job ); + hb_job_dereference( job ); while( ( j = hb_job( h, index ) ) && (j->sequence_id != 0) ) + { hb_rem( h, j ); + hb_job_dereference( j ); + } return; } else @@ -1163,7 +1182,9 @@ if (s->state == HB_STATE_WORKING) { NSString * msg; - if (job->pass == -1) + if (!job) + msg = @""; // no current job, even though state says working. Means there's a transition from one job to another going on. + else if (job->pass == -1) msg = NSLocalizedString( @"Deep Scan", nil ); else if (job->pass == 1) msg = NSLocalizedString( @"Analyzing video", nil );