简体   繁体   中英

Memory Leak Windows Phone

I'm parsing a KML file to display a route but I ran out of memory and application terminates can somebody help me solve this problem

public void DisplayRoute(int i)
        {
            content = new KmlContent();
            info = Application.GetResourceStream(new Uri("/AppStudio;Component/Resources/kml", UriKind.Relative));
            data = content.DeserializeKml(info.Stream);
           if (data.Document.Placemarks[i].LineString != null)
                {
                        routeQuery = new RouteQuery();
                        routeQuery.Waypoints = content.ParseLocation(data.Document.Placemarks[i].LineString.Coordinates);
                        routeQuery.QueryAsync(); 
                        routeQuery.QueryCompleted += routeQuery_QueryCompleted;
                }
           else
           {
               return;
           }
        }

        int count = 0;
        public void routeQuery_QueryCompleted(object sender, QueryCompletedEventArgs<Route> e)
        {
            if (e.Error == null)
            {
                Route MyRoute = e.Result;
                MapRoute mappedRoute = new MapRoute(MyRoute);
                MainMap.AddRoute(mappedRoute);
                MainMap.SetView(mappedRoute.Route.BoundingBox);
                routeQuery.Dispose();
                count++;
            }
            DisplayRoute(count);
        }

for the first run the route is displayed, as I navigate to the start page and then back to Map I ran out of memory

The issue is that you never release the stream you get from Application.GetResourceStream . Change your code to

using (info = Application.GetResourceStream(new Uri("/AppStudio;Component/Resources/kml", UriKind.Relative)))
{
    //rest of the code using info variable
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM