From 402fa1d53531bd911eaacddca109d5d551ac7c2d Mon Sep 17 00:00:00 2001
From: Carl Worth <cworth@cworth.org>
Date: Wed, 30 Oct 2013 14:35:21 -0700
Subject: [PATCH] Fix resource leaks when switching contexts.

Previously, fips was already freeing memory that it had allocated for
its own linked lists of outstanding queries when switching contexts.

In addition, in this commit, we also now call End on any active
timer-query/performance-monitor and then call Delete on all queries
for which we have not previously collected results.

This avoids leaks within the OpenGL implementation as it holds on to
results that fips will never ask for.
---
 metrics.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/metrics.c b/metrics.c
index 9b94eb6..87da778 100644
--- a/metrics.c
+++ b/metrics.c
@@ -332,6 +332,8 @@ metrics_info_fini (void)
 		return;
 
 	if (ctx->timer_begun_id) {
+		glEndQuery (GL_TIME_ELAPSED);
+		glDeleteQueries (1, &ctx->timer_begun_id);
 		ctx->timer_begun_id = 0;
 	}
 
@@ -339,6 +341,7 @@ metrics_info_fini (void)
 	     timer;
 	     timer = timer_next)
 	{
+		glDeleteQueries (1, &timer->id);
 		timer_next = timer->next;
 		free (timer);
 	}
@@ -346,6 +349,8 @@ metrics_info_fini (void)
 	ctx->timer_tail = NULL;
 
 	if (ctx->monitor_begun_id) {
+		glEndPerfMonitorAMD (ctx->monitor_begun_id);
+		glDeletePerfMonitorsAMD (1, &ctx->monitor_begun_id);
 		ctx->monitor_begun_id = 0;
 	}
 
@@ -353,6 +358,7 @@ metrics_info_fini (void)
 	     monitor;
 	     monitor = monitor_next)
 	{
+		glDeletePerfMonitorsAMD (1, &monitor->id);
 		monitor_next = monitor->next;
 		free (monitor);
 	}
-- 
2.45.2