Index: gstmotioncells.c
===================================================================
--- gstmotioncells.c	(revision 8503)
+++ gstmotioncells.c	(revision 9027)
@@ -142,33 +142,27 @@
 	motion_cells_free(filter->id);
 
 	//freeing previously allocated overlay region array
-	if (filter->motionmaskcoord_count > 0) {
+	if (filter->motionmaskcoord_count > 0)
 	    g_free(filter->motionmaskcoords);
-	}
-
-	if(filter->motionmaskcells_count>0){
+	if (filter->motionmaskcells_count > 0)
 	    g_free(filter->motionmaskcellsidx);
-	}
-	if (filter->motioncells_count > 0) {
+	if (filter->motioncells_count > 0)
 	    g_free(filter->motioncellsidx);
-	}
-
-	if (filter->cvImage) {
+	if (filter->cvImage)
 		cvReleaseImage(&filter->cvImage);
-	}
-
-	if(filter->prev_datafile)
+	if (filter->prev_datafile)
 		g_free(filter->prev_datafile);
-	if(filter->cur_datafile)
+	if (filter->cur_datafile)
 		g_free(filter->cur_datafile);
-	if(filter->basename_datafile)
+	if (filter->basename_datafile)
 		g_free(filter->basename_datafile);
-	if(filter->datafile_extension)
+	if (filter->datafile_extension)
 		g_free(filter->datafile_extension);
-	g_free(filter->motioncellscolor);
+	if (filter->motioncellscolor)
+		g_free(filter->motioncellscolor);
+	if (filter->propset_mutex)
+		g_mutex_free (filter->propset_mutex);
 
-	g_mutex_free (filter->changed_datafile_mutex);
-
 	G_OBJECT_CLASS(parent_class)->finalize(obj);
 }
 
@@ -326,7 +320,7 @@
 	filter->starttime = 1000 * filter->tv.tv_sec;
 	filter->previous_motion = false;
 	filter->changed_datafile = false;
-	filter->changed_datafile_mutex = g_mutex_new();
+	filter->propset_mutex = g_mutex_new();
 	filter->postallmotion = false;
 	filter->usealpha = false;
 	filter->firstdatafile = false;
@@ -338,9 +332,11 @@
 	filter->changed_startime = false;
 	filter->sent_init_error_msg = false;
 	filter->sent_save_error_msg = false;
 	filter->thickness = THICKNESS_DEF;
 
 	filter->datafileidx = 0;
 	filter->id = instanceCounter;
 	motion_cells_init();
 
@@ -361,7 +357,8 @@
 
 	switch (prop_id) {
 	case PROP_GRID_X:
-		 filter->ret= gst_element_get_state (filter,
+		g_mutex_lock(filter->propset_mutex);
+		 filter->ret= gst_element_get_state (GST_ELEMENT (filter),
 		      &filter->state, &filter->pending, 250 * GST_NSECOND);
 		filter->gridx = g_value_get_int(value);
 		if(filter->prevgridx != filter->gridx && filter->ret == GST_STATE_CHANGE_SUCCESS && filter->state == GST_STATE_PLAYING){
@@ -370,9 +367,11 @@
 		}else{
 			filter->prevgridx = filter->gridx;
 		}
+		g_mutex_unlock(filter->propset_mutex);
 		break;
 	case PROP_GRID_Y:
-		filter->ret= gst_element_get_state (filter,
+		g_mutex_lock(filter->propset_mutex);
+		filter->ret= gst_element_get_state (GST_ELEMENT (filter),
 				      &filter->state, &filter->pending, 250 * GST_NSECOND);
 		filter->gridy = g_value_get_int(value);
 		if(filter->prevgridy != filter->gridy && filter->ret == GST_STATE_CHANGE_SUCCESS && filter->state == GST_STATE_PLAYING){
@@ -381,30 +380,46 @@
 		}else{
 			filter->prevgridy = filter->gridy;
 		}
+		g_mutex_unlock(filter->propset_mutex);
 		break;
 	case PROP_GAP:
+		g_mutex_lock(filter->propset_mutex);
 		filter->gap = g_value_get_int(value);
+		g_mutex_unlock(filter->propset_mutex);
 		break;
 	case PROP_SENSITIVITY:
+		g_mutex_lock(filter->propset_mutex);
 		filter->sensitivity = g_value_get_double(value);
+		g_mutex_unlock(filter->propset_mutex);
 		break;
 	case PROP_THRESHOLD:
+		g_mutex_lock(filter->propset_mutex);
 		filter->threshold = g_value_get_double(value);
+		g_mutex_unlock(filter->propset_mutex);
 		break;
 	case PROP_DISPLAY:
+		g_mutex_lock(filter->propset_mutex);
 		filter->display = g_value_get_boolean(value);
+		g_mutex_unlock(filter->propset_mutex);
 		break;
 	case PROP_POSTALLMOTION:
+		g_mutex_lock(filter->propset_mutex);
 		filter->postallmotion = g_value_get_boolean(value);
+		g_mutex_unlock(filter->propset_mutex);
 		break;
 	case PROP_USEALPHA:
+		g_mutex_lock(filter->propset_mutex);
 		filter->usealpha = g_value_get_boolean(value);
+		g_mutex_unlock(filter->propset_mutex);
 		break;
 	case PROP_CALCULATEMOTION:
+		g_mutex_lock(filter->propset_mutex);
 		filter->calculate_motion = g_value_get_boolean(value);
+		g_mutex_unlock(filter->propset_mutex);
 		break;
 	case PROP_DATE:
-		filter->ret= gst_element_get_state (filter,
+		g_mutex_lock(filter->propset_mutex);
+		filter->ret= gst_element_get_state (GST_ELEMENT (filter),
 						      &filter->state, &filter->pending, 250 * GST_NSECOND);
 		if(filter->ret == GST_STATE_CHANGE_SUCCESS && filter->state == GST_STATE_PLAYING){
 			filter->starttime = g_value_get_long(value);
@@ -412,6 +427,7 @@
 		}else{
 			filter->starttime = g_value_get_long(value);
 		}
+		g_mutex_unlock(filter->propset_mutex);
 		break;
 	case PROP_DATAFILE:
 		if(filter->cur_datafile)
@@ -419,34 +435,39 @@
 		if(filter->basename_datafile)
 			g_free(filter->basename_datafile);
 		filter->basename_datafile = g_value_dup_string(value);
-
 		if(strlen(filter->basename_datafile) == 0){
 			filter->cur_datafile = g_strdup("");
 			break;
 		}
 
 		filter->cur_datafile = g_strdup_printf("%s-0.%s",filter->basename_datafile,filter->datafile_extension);

 		if(g_strcmp0(filter->prev_datafile,filter->basename_datafile) != 0)	{
-			g_mutex_lock(filter->changed_datafile_mutex);
+			g_mutex_lock(filter->propset_mutex);
 			filter->changed_datafile = TRUE;
+			motion_cells_free_resources(filter->id);
 			filter->sent_init_error_msg = FALSE;
 			filter->sent_save_error_msg = FALSE;
+			filter->sent_searchidx_error_msg = FALSE;
 			filter->datafileidx = 0;
-			motion_cells_free_resources(filter->id);
-			g_mutex_unlock(filter->changed_datafile_mutex);
+			g_mutex_unlock(filter->propset_mutex);
 		}
 
 		if (filter->prev_datafile)
 			g_free(filter->prev_datafile);
 		filter->prev_datafile = g_strdup (filter->basename_datafile);
 		break;
 	case PROP_DATAFILE_EXT:
+		g_mutex_lock(filter->propset_mutex);
 		if (filter->datafile_extension)
 			g_free(filter->datafile_extension);
 		filter->datafile_extension = g_value_dup_string(value);
+		g_mutex_unlock(filter->propset_mutex);
 		break;
 	case PROP_MOTIONMASKCOORD:
+		g_mutex_lock(filter->propset_mutex);
 		strs = g_strsplit(g_value_get_string(value), ",", 255);
 
 		//setting number of regions
@@ -471,8 +492,10 @@
 		tmpuy = -1;
 		tmplx = -1;
 		tmply = -1;
+		g_mutex_unlock(filter->propset_mutex);
 		break;
 	case PROP_MOTIONMASKCELLSPOS:
+		g_mutex_lock(filter->propset_mutex);
 		motionmaskcellsstr = g_strsplit(g_value_get_string(value), ",", 255);
 
 		//setting number of regions
@@ -495,9 +518,10 @@
 		tmpuy = -1;
 		tmplx = -1;
 		tmply = -1;
+		g_mutex_unlock(filter->propset_mutex);
 		break;
 	case PROP_CELLSCOLOR:
-
+		g_mutex_lock(filter->propset_mutex);
 		colorstr = g_strsplit(g_value_get_string(value), ",", 255);
 		sscanf(colorstr[0], "%d", &r);
 		sscanf(colorstr[1], "%d", &g);
@@ -510,8 +534,10 @@
 		filter->motioncellscolor->G_channel_value = g;
 		filter->motioncellscolor->B_channel_value = b;
 		if (colorstr) g_strfreev(colorstr);
+		g_mutex_unlock(filter->propset_mutex);
 		break;
 	case PROP_MOTIONCELLSIDX:
+		g_mutex_lock(filter->propset_mutex);
 		motioncellsstr = g_strsplit(g_value_get_string(value), ",", 255);
 
 		//setting number of regions
@@ -539,9 +565,12 @@
 		tmpuy = -1;
 		tmplx = -1;
 		tmply = -1;
+		g_mutex_unlock(filter->propset_mutex);
 		break;
 	case PROP_MOTIONCELLTHICKNESS:
+		g_mutex_lock(filter->propset_mutex);
 		filter->thickness = g_value_get_int(value);
+		g_mutex_unlock(filter->propset_mutex);
 		break;
 	default:
 		G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
@@ -685,6 +714,8 @@
 	gst_structure_get_int(structure, "height", &height);
 	gst_structure_get_fraction(structure,"framerate",&numerator,&denominator);
 	filter->framerate=(double)numerator/(double)denominator;
+	if(filter->cvImage)
+		cvReleaseImage(&filter->cvImage);
 	filter->cvImage = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 3);
 
 	otherpad = (pad == filter->srcpad) ? filter->sinkpad : filter->srcpad;
@@ -702,7 +733,7 @@
 
 	filter = GST_MOTIONCELLS (GST_OBJECT_PARENT (pad));
 	if(filter->calculate_motion){
-		g_mutex_lock(filter->changed_datafile_mutex);
+		g_mutex_lock(filter->propset_mutex);
 		buf = gst_buffer_make_writable(buf);
 		filter->cvImage->imageData = (char *) GST_BUFFER_DATA(buf);
 		if(filter->firstframe){
@@ -717,7 +748,6 @@
 		bool display = filter->display;
 		int motionmaskcoord_count = filter->motionmaskcoord_count;
 
 		motionmaskcoordrect *motionmaskcoords =(motionmaskcoordrect*)g_malloc(filter->motionmaskcoord_count * sizeof(motionmaskcoordrect));
 		int i = 0;
 		for(i=0;i<filter->motionmaskcoord_count;i++){
@@ -741,12 +771,13 @@
 		}
 
 		if((g_strcmp0(filter->cur_datafile," ") != 0) &&
-				(filter->changed_gridx || filter->changed_gridy) || filter->changed_startime){
+				(filter->changed_gridx || filter->changed_gridy || filter->changed_startime)){
 			if (filter->cur_datafile)
 				g_free(filter->cur_datafile);
 			filter->datafileidx++;
 			filter->cur_datafile = g_strdup_printf("%s-%d.%s",filter->basename_datafile,filter->datafileidx,filter->datafile_extension);
 			filter->changed_datafile = TRUE;
+			motion_cells_free_resources(filter->id);
 			filter->changed_gridx = FALSE;
 			filter->changed_gridy = FALSE;
 			filter->changed_startime = FALSE;
@@ -842,7 +900,7 @@
 		g_free(motionmaskcellsidx);
 		g_free(motioncellsidx);
 
-		g_mutex_unlock(filter->changed_datafile_mutex);
+		g_mutex_unlock(filter->propset_mutex);
 	}
 
 	return gst_pad_push(filter->srcpad, buf);
Index: gstmotioncells.h
===================================================================
--- gstmotioncells.h	(revision 8503)
+++ gstmotioncells.h	(revision 9027)
@@ -82,7 +82,7 @@
   GstState state,pending;
   GstStateChangeReturn ret;
   gboolean display,calculate_motion,firstgridx,firstgridy,changed_gridx,changed_gridy,changed_startime;
   gboolean sent_init_error_msg,sent_save_error_msg;

   gchar *prev_datafile,*cur_datafile,*basename_datafile,*datafile_extension;
   gint prevgridx,gridx;
   gint prevgridy,gridy;
@@ -94,14 +94,14 @@
   cellscolor *motioncellscolor;
   motioncellidx* motioncellsidx,*motionmaskcellsidx;
   int motionmaskcoord_count,motioncells_count,motionmaskcells_count;
   int gap,thickness,datafileidx;
   long int motion_begin_timestamp,last_motion_timestamp,motion_timestamp;
   long int prev_buff_timestamp,cur_buff_timestamp,diff_timestamp;
   gint64 starttime;
   //time stuff
   struct timeval tv;
   gboolean previous_motion,changed_datafile,postallmotion,usealpha,firstdatafile,firstframe;
-  GMutex *changed_datafile_mutex;
+  GMutex *propset_mutex;
   double framerate;
 };

