George
George

Reputation: 11

AS3/Flash BitmapData .draw memory leak

I have a large sprite...and I'm splitting it into several smaller ones with function bellow. The function works great but the memory usage of my .swf grows dramatically.

What am I doing wrong?

private function split_sprite(sp:Sprite,the_parrent:Sprite) {


    var region:Rectangle = new Rectangle();
    region = sp.getBounds(the_parrent);

    var w=region.width/10;
    var h = region.height ;

    for (var i=0;i<10;i++){ 
            //Build Matrix Transform
            var mat:Matrix= new Matrix(1,0,0,1, 0-w*i, 0 );
            sp.transform.matrix = mat;

            //Draw the bitmap
        var temp:BitmapData = new BitmapData(w + 2 , h, true, 0x000000);


            temp.draw(sp,mat);

            //Attach the BitmapData to a Bitmap Instance
            var myBitmap:Bitmap = new Bitmap(temp);
            myBitmap.smoothing = true;
            //Re apply an Inverse Matrix
            mat = new Matrix(1,0,0,1, region.x, region.y);
            myBitmap.transform.matrix = mat;
            var spp=new Sprite();
             //spp.cacheAsBitmap=true;
            spp.addChild(myBitmap);
            spp.x=w*i;
            the_parrent.addChild(spp);


    }



}

Upvotes: 1

Views: 2569

Answers (1)

Myk
Myk

Reputation: 6215

If I remember correctly, BitmapData will never be garbage collected - you are creating a lot of new BMD objects, but when you are done with them you have to call myBitmapDataObject.dispose() to clean up after yourself. I dunno if that'll help - you don't have a lot of details on the nature of this memroy leak - but that's one place to look. BitmapData objects can eat up a lot of memory fast if you're not dispose()ing properly.

Upvotes: 4

Related Questions