x


help with memory leak

first to clear something out, the function i wrote is probably not the best practice, but it was done fast and it works, but later i discovered that memory used increases every time i use this function...so i guess there is some kind of problem with arrays... can you help me out. memory used increases pretty fast it renders function useless....

here is the code:

//REMOVE ALL OBJECTS THAT ARE OUTSIDE CUT OFF DISTANCE
function RemoveObjectsOutsideCutoffDistance(center : Vector3){

//ONLY REMOVE IF WE ARE IN NORMAL MODE
if(currentViewState==viewState.normal || currentViewState==viewState.region){       

  var objectsToShow=new Array();

//GO THROUGH ALL SYSTEMS
for(i=0;i<TrackingControl.Systems.length;i++){          
    //CHECK IF SYSTEM IS LOADED AND VISIBLE
    if(TrackingControl.Systems[i].isLoaded && TrackingControl.Systems[i].isVisible){            
        //GET THE COLLIDERS OF OBJECTS THAT ARE INSIDE THE SPHERE COLLIDER
        var colliders=Physics.OverlapSphere(center,TrackingControl.Systems[i].radius);              

        //MAKE AN ARRAY OF OBJECTS THAT WE ARE GOING TO SHOW                
        for(var e=0;e<colliders.length;e++){
            if(colliders[e].tag==TrackingControl.Systems[i].tag){
                if(!colliders[e].gameObject.GetComponent("Highlight").hidden){
                    objectsToShow.Push(colliders[e].transform);
                }
            }
        }

        //HIDE ALL OBJECTS IN THE SYSTEM
        var Objects : GameObject[]=GameObject.FindGameObjectsWithTag(TrackingControl.Systems[i].tag);
        for(k=0;k<Objects.length;k++){
            Objects[k].renderer.enabled=false;
            Objects[k].gameObject.layer=2;
        }
    }       
        }

    //COPY "OBJECTS TO SHOW" INTO GLOBAL ARRAY THAT TRACKS VISIBLE OBJECTS IN THE SCENE
    //WE CAN USE THAT ARRAY TO HANDLE WHAT OBJECT TO SHOW WHEN IN HIDDEN MODE
    if(currentSeeThroughState==seeThroughState.hidden){
        visibleObjects=objectsToShow;
    }

    //UNHIDE THE ONES THAT ARE INSIDE THE SPHERE
    for(j=0;j<objectsToShow.length;j++){
        if(!objectsToShow[j].gameObject.GetComponent("Highlight").hidden){
            objectsToShow[j].renderer.enabled=true;
            objectsToShow[j].gameObject.layer=0;
            RemoveHighlight(objectsToShow[j].transform);
        }
    }
}

}

what is happening here is that i want to remove all objects from the scene that are not inside the sphere that has radius determined in other script. so i get all objects (from different systems in the scene- tracking control) that are in the sphere, copy all the objects in the array objectsToShow, then hide all objects in the scene and then unhide that ones that are in the objectsToShow. There is also a case where we copy that array to other global array, but that is irrelevant now, since memory goes up when we are outside that state.

it works fine but only things that worries me is that memory leaks....i do not know why, do i need to clear arrays? they should be alive only inside the function.

thanks in advance! any help appreciated!

cross post to forums: http://forum.unity3d.com/threads/79548-problem-with-memory-leak..-please-help-me-solve-it

more ▼

asked Feb 25 '11 at 01:14 PM

pretender gravatar image

pretender
497 121 134 145

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

You want to not render objects outside a given distance/radius?

Why not use the farClipPlane of your Camera for all objects or layerCullDistances for several objects assigned to layers?

more ▼

answered Feb 25 '11 at 02:30 PM

efge gravatar image

efge
5.1k 5 14 38

because i want to remove object that are outside of sphere radius which is in the center of current selection not camera

Feb 25 '11 at 04:35 PM pretender
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1363
x328
x32
x31

asked: Feb 25 '11 at 01:14 PM

Seen: 1398 times

Last Updated: Feb 25 '11 at 01:14 PM