Index: gst/gstelementfactory.c
===================================================================
RCS file: /cvs/gstreamer/gstreamer/gst/gstelementfactory.c,v
retrieving revision 1.133
diff -u -p -u -p -r1.133 gstelementfactory.c
--- gst/gstelementfactory.c	25 Jul 2007 18:37:12 -0000	1.133
+++ gst/gstelementfactory.c	11 Sep 2007 06:34:29 -0000
@@ -217,10 +231,22 @@ gst_element_factory_cleanup (GstElementF
 
   for (item = factory->staticpadtemplates; item; item = item->next) {
     GstStaticPadTemplate *templ = item->data;
+    GstCaps *caps = (GstCaps *) &(templ->static_caps);
 
     g_free (templ->name_template);
     g_free ((gchar *) templ->static_caps.string);
-    memset (&(templ->static_caps), 0, sizeof (GstStaticCaps));
+    if(g_atomic_int_get (&caps->refcount) > 0) {
+      GstStructure *structure;
+      guint i;
+
+      for (i = 0; i < caps->structs->len; i++) {
+        structure = (GstStructure *) gst_caps_get_structure (caps, i);
+        gst_structure_set_parent_refcount (structure, NULL);
+        gst_structure_free (structure);
+      }
+      g_ptr_array_free (caps->structs, TRUE);
+      g_atomic_int_set (&caps->refcount, 0);
+    }
     g_free (templ);
   }
   g_list_free (factory->staticpadtemplates);
