Ultrashock Forums > Flash > ActionScript
AS3 - BitmapData memory leak is AS3

You are currently viewing our website as a guest which gives you limited access to forums, files and other resources.

Click here to join now for free, and start interacting with our members, download files and much more!

Click here if you are looking for our Flash files and other professional assets.
 
Post Reply | View first unread | Rate Thread Search this Thread | Thread Tools | Display Modes

#1
Bookmark and Share!
AS3 - BitmapData memory leak is AS3
Old 2008-03-25

I have a problem with drawing bitmapData in AS3 with an onEnterFrame action. It worked great in AS2, and now that I converted it to AS3, it's bogging down on my system rather slowly.... I'm using the dispose() method on both bitmapData instances I have, and I can only notice the slowness after about 10 minutes of playing it...

Hope somone has an insight about the issue. Here is a snippet of my code:

Code:
function enterFrameEvent(e:Event) {    
    makeKlydo();
}

function makeKlydo(){
    
    stampImage = new BitmapData(this.myElement["k"+myElementObject].width, this.myElement["k"+myElementObject].height, true, 0xFFFFFF);
    
    stampImage.draw(myElement, new Matrix(1, 0, 0, 1, this.myElement["k"+myElementObject].width/2, this.myElement["k"+myElementObject].height));
    if (deceleration<1){
        deceleration -= (deceleration-1)/5;
    }
    
    if (rotate1) {
        r += rotspeed1 * fanning * deceleration;
    }
    
    if (rotate2) {
        r2r -= rotspeed2 * fanning * deceleration;
    }
    
    if (rotate3) {
        rot += rotspeed3 * fanning;
    }
    
    map.dispose();
    map = new BitmapData(hsize, vsize, true, 0x00000000);
    mapBM = new Bitmap(map);
    mapHolder.addChild(mapBM);

    for (var i = 0; i<slices; i++) {
        m.identity();
        m.b += sh1;
        m.c += sh2;
        m.rotate(r2r);
        m.translate(myElement.width/2,myElement.height);
        m.rotate(r);
        m.scale(scl, scl);
        slice.graphics.clear();
        slice.graphics.lineStyle();
        slice.graphics.moveTo(0, 0);
        slice.graphics.beginBitmapFill(stampImage, m);
        slice.graphics.lineTo(Math.cos((angle+nudge)-Math.PI/2)*diag, Math.sin((angle+nudge)-Math.PI/2)*diag);
        slice.graphics.lineTo(Math.cos(-(angle+nudge)-Math.PI/2)*diag, Math.sin(-(angle+nudge)-Math.PI/2)*diag);
        slice.graphics.lineTo(0, 0);
        slice.graphics.endFill();
        m.identity();
        if (flip && i%2 == 1) {
            m.scale(-1, 1);
        }
        //m.rotate(rot+i*angle*2*fanning);
        m.rotate(rot+i*angle*2);
        m.translate(hsize*0.5, vsize*0.5);
        
        map.draw(slice, m, null, "normal", null, true);        
    }
        stampImage.dispose();
}

addEventListener(Event.ENTER_FRAME, enterFrameEvent);
thank!
::| ITAI |::
postbit arrow 3 comments | 4489 views postbit arrow Reply: with Quote   
Registered User
iasseo is offline
seperator
Posts: 120
2003-02-21
iasseo lives in United States
seperator

Ultrashock Member Comments:
Nutrox's Avatar Nutrox Nutrox is offline Super Moderator Nutrox lives in United Kingdom 17 Creative Assets 2008-03-25 #2 Old  
A couple of things...

First of all it looks like you are adding a new Bitmap object to mapHolder on every frame, but you aren't removing any of them as far as I can tell, so mapHolder is just going to end up with a zillion Bitmap objects attached to it. Instead of creating a new Bitmap object each time you could just use the Bitmap.bitmapData property to change the BitmapData the Bitmap is displaying.

You are also creating new BitmapData objects on each frame but you aren't using any of the BitmapData methods that would improve rendering times. BitmapData.draw() uses the vector renderer, so unless I have missed something the use of BitmapData objects seems a bit excessive for what you are doing there.

Reply With Quote  
iasseo iasseo is offline iasseo lives in United States 2008-03-25 #3 Old  
that makes sense... I'll fix those issues and see if it's still boging down the system. thanks.
Reply With Quote  
iasseo iasseo is offline iasseo lives in United States 2008-03-26 #4 Old  
Well - this is the problem -- when I move the constructor methods outside of the enterFrame function (Klydo), the dispose() Method throws an error:

ArgumentError: Error #2015: Invalid BitmapData.
at flash.display::BitmapData/draw()
at klydo_as3_v3_fla::MainTimeline/makeKlydo()
at klydo_as3_v3_fla::MainTimeline/enterFrameEvent()
Reply With Quote  
Thread Tools
Display Modes Rate This Thread
Rate This Thread: