OutOfMemoryException with RaySceneQuery

dumpbyte

19-09-2011 14:09:13

Hi guys.

I've implemented in my Mogre application a "mouse-over" functionality, in order to detect what objects are under the mouse pointer. This functionality is executed multiple times per frame, always in the FrameRenderingQueued event.

However, when running the application and observe the Windows TaskManager, I can see how the application is growing in use of memory, until finally I get an OutOfMemoryException.

I'm sure the problem is in this function, as I have not encountered such problems in other applications without this feature .

The code I use is is the following:




Point position;
Point relativeToForm;
Ray mouseRay;
RaySceneQueryResult rayResult;
List<string> pickedObjects;
System.Collections.Generic.IEnumerator<RaySceneQueryResultEntry> itr;

public List<string> GetObjectsUnderCursor()
{

using (RaySceneQuery myRaySceneQuery = myM3DEngine.MogreSceneManager.CreateRayQuery(new Ray()))
{

position = System.Windows.Forms.Cursor.Position;
relativeToForm = myM3DEngine.RenderManager.TargetRenderWindow.PointToClient(position);

float X = (float)relativeToForm.X / (float)myM3DEngine.MogreViewport.ActualWidth;
float Y = (float)relativeToForm.Y / (float)myM3DEngine.MogreViewport.ActualHeight;


mouseRay = myM3DEngine.SceneManager.MogreCamera.GetCameraToViewportRay(X, Y);
myRaySceneQuery.Ray = mouseRay;
myRaySceneQuery.SetSortByDistance(true);
rayResult = myRaySceneQuery.Execute();
itr = rayResult.GetEnumerator();

pickedObjects = new List<string>();


while (itr != null && itr.MoveNext())
{
if (itr.Current.movable != null)
{
pickedObjects.Add(itr.Current.movable.Name);


}

}


myRaySceneQuery.ClearResults();
myRaySceneQuery.Dispose();
rayResult.Dispose();
itr.Dispose();


return pickedObjects;

}



}



Can you tell me guys what is wrong here?

Thanks in advance.

smiley80

19-09-2011 16:59:29

You have to destroy the query:
myM3DEngine.SceneManager.DestroyQuery(myRaySceneQuery);

Btw. instead of:
itr = rayResult.GetEnumerator();

while (itr != null && itr.MoveNext())
{
if (itr.Current.movable != null)
{
pickedObjects.Add(itr.Current.movable.Name);
}
}
itr.Dispose();

you can use:
foreach (var entry in rayResult)
{
if (entry.movable != null)
{
pickedObjects.Add(entry.movable.Name);
}
}


That's redundant:
myRaySceneQuery.Dispose();

dumpbyte

19-09-2011 18:21:05

Guys... I am still proud to say that this community works much better than many professional support services :lol:
Thank you for your help, it seems that now works perfect.