Index: src/hg/hgTables/intersect.c
===================================================================
RCS file: /projects/compbio/cvsroot/kent/src/hg/hgTables/intersect.c,v
retrieving revision 1.51
retrieving revision 1.52
diff -b -B -U 4 -r1.51 -r1.52
--- src/hg/hgTables/intersect.c	20 May 2009 20:59:56 -0000	1.51
+++ src/hg/hgTables/intersect.c	4 Jan 2010 19:12:22 -0000	1.52
@@ -621,11 +621,13 @@
     if (sameString(op, "cat"))
 	{
 	struct bed *bedList = getRegionAsBed(db, table, region, filter,
 					     idHash, lm, retFieldCount);
-	for (subtrack = curTrack->subtracks; subtrack != NULL;
-	     subtrack = subtrack->next)
+	struct slRef *tdbRefList = trackDbListGetRefsToDescendantLeaves(curTrack->subtracks);
+	struct slRef *tdbRef;
+	for (tdbRef = tdbRefList; tdbRef != NULL; tdbRef = tdbRef->next)
 	    {
+	    subtrack = tdbRef->val;
 	    if (! sameString(curTable, subtrack->tableName) &&
 		isSubtrackMerged(subtrack->tableName) &&
 		sameString(subtrack->type, primaryType))
 		{
@@ -634,8 +636,9 @@
 				   idHash, lm, retFieldCount);
 		bedList = slCat(bedList, bedList2);
 		}
 	    }
+	slFreeList(&tdbRefList);
 	return bedList;
 	}
     bits1 = bitAlloc(chromSize+8);
     bits2 = bitAlloc(chromSize+8);
@@ -651,11 +654,13 @@
 					      idHash, lm2, retFieldCount);
 	bedOrBits(bits1, chromSize, bedList1, hti->hasBlocks, 0);
 	lmCleanup(&lm2);
 	}
-    for (subtrack = curTrack->subtracks; subtrack != NULL;
-	 subtrack = subtrack->next)
+    struct slRef *tdbRefList = trackDbListGetRefsToDescendantLeaves(curTrack->subtracks);
+    struct slRef *tdbRef;
+    for (tdbRef = tdbRefList; tdbRef != NULL; tdbRef = tdbRef->next)
 	{
+	subtrack = tdbRef->val;
 	if (! sameString(curTable, subtrack->tableName) &&
 	    isSubtrackMerged(subtrack->tableName) &&
 	    sameString(subtrack->type, primaryType))
 	    {
@@ -675,8 +680,9 @@
 		bitOr(bits1, bits2, chromSize);
 	    lmCleanup(&lm2);
 	    }
 	}
+    slFreeList(&tdbRefList);
     if (isBpWise)
 	{
 	bedMerged = bitsToBed4List(bits1, chromSize, region->chrom, 1,
 				   region->start, region->end, lm);

