Index: gstlame.c
===================================================================
RCS file: /cvs/gstreamer/gst-plugins-ugly/ext/lame/gstlame.c,v
retrieving revision 1.87
diff -u -r1.87 gstlame.c
--- gstlame.c	16 Feb 2006 17:16:06 -0000	1.87
+++ gstlame.c	4 Mar 2006 11:30:28 -0000
@@ -66,6 +66,7 @@
         "channels = (int) [ 1, 2 ]")
     );
 
+
 /********** Define useful types for non-programmatic interfaces **********/
 #define GST_TYPE_LAME_MODE (gst_lame_mode_get_type())
 static GType
@@ -276,14 +277,28 @@
 }
 
 static void
-gst_lame_finalize (GObject * obj)
+gst_lame_release_memory (GstLame *lame)
 {
-  GstLame *lame = GST_LAME (obj);
-
   g_slist_foreach (lame->tag_strings, (GFunc) g_free, NULL);
   g_slist_free (lame->tag_strings);
   lame->tag_strings = NULL;
 
+  if (lame->tags) {
+    gst_tag_list_free (lame->tags);
+    lame->tags = NULL;
+  }
+
+  if (lame->lgf) {
+    lame_close (lame->lgf);
+    lame->lgf = NULL;
+  }
+}
+
+static void
+gst_lame_finalize (GObject * obj)
+{
+  gst_lame_release_memory (GST_LAME (obj));
+
   G_OBJECT_CLASS (parent_class)->finalize (obj);
 }
 
@@ -466,29 +481,34 @@
   /* and use these caps */
   gst_pad_set_caps (lame->srcpad, othercaps);
   gst_caps_unref (othercaps);
+  gst_object_unref (lame);
 
   return TRUE;
 
 no_rate:
   {
+    gst_object_unref (lame);
     GST_ELEMENT_ERROR (lame, CORE, NEGOTIATION, (NULL),
         ("no rate specified in input"));
     return FALSE;
   }
 no_channels:
   {
+    gst_object_unref (lame);
     GST_ELEMENT_ERROR (lame, CORE, NEGOTIATION, (NULL),
         ("no channels specified in input"));
     return FALSE;
   }
 zero_output_rate:
   {
+    gst_object_unref (lame);
     GST_ELEMENT_ERROR (lame, CORE, NEGOTIATION, (NULL),
         ("lame decided on a zero sample rate"));
     return FALSE;
   }
 setup_failed:
   {
+    gst_object_unref (lame);
     GST_ELEMENT_ERROR (lame, CORE, NEGOTIATION, (NULL),
         ("could not initialize encoder (wrong parameters?)"));
     return FALSE;
@@ -499,18 +519,19 @@
 gst_lame_init (GstLame * lame)
 {
   GST_DEBUG_OBJECT (lame, "starting initialization");
+  GstPadTemplate *template;
 
-  lame->sinkpad =
-      gst_pad_new_from_template (gst_static_pad_template_get
-      (&gst_lame_sink_template), "sink");
+  template = gst_static_pad_template_get (&gst_lame_sink_template);
+  lame->sinkpad = gst_pad_new_from_template (template, "sink");
+  gst_object_unref (template);
   gst_pad_set_event_function (lame->sinkpad, gst_lame_sink_event);
   gst_pad_set_chain_function (lame->sinkpad, gst_lame_chain);
   gst_pad_set_setcaps_function (lame->sinkpad, gst_lame_sink_setcaps);
   gst_element_add_pad (GST_ELEMENT (lame), lame->sinkpad);
 
-  lame->srcpad =
-      gst_pad_new_from_template (gst_static_pad_template_get
-      (&gst_lame_src_template), "src");
+  template = gst_static_pad_template_get (&gst_lame_src_template);
+  lame->srcpad = gst_pad_new_from_template (template, "src");
+  gst_object_unref (template);
   gst_element_add_pad (GST_ELEMENT (lame), lame->srcpad);
 
   /* create an encoder state so we can ask about defaults */
@@ -975,6 +996,7 @@
       gst_pad_push_event (lame->srcpad, event);
       break;
   }
+  gst_object_unref (lame);
   return TRUE;
 }
 
@@ -1194,7 +1216,7 @@
 
   switch (transition) {
     case GST_STATE_CHANGE_READY_TO_NULL:
-      gst_tag_list_free (lame->tags);
+      gst_lame_release_memory (lame);
       break;
     default:
       break;
