Reputation: 2991
I have a very complex Flash application (think Photoshop in Flash).
There is a lot of image manipulation, and all is working well with the exception of one bug.
The application has an undo/redo feature that sometimes throws an error. There is way too much code to paste here, but the issue revolves around this line of code:
trace("UNDO BMD: " + BeautyGlobal.undoArray[_undoSteps].bitmapData);
var newUndoData:BitmapData = BeautyGlobal.undoArray[_undoSteps].bitmapData.clone()
The trace statement displays:
UNDO BMD: [object BitmapData]
ArgumentError: Error #2015: Invalid BitmapData.
at flash.display::BitmapData/clone()
at Main/undoAction()
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at com.beautyCanvas.ui::Toolbar/undoClick()
So there is bitmapdata there, but it is not valid. This issue doesn't preset itself all of the time. If I could detect if the bitmap data is valid, I can stop it from crashing the entire application. I have tried a few things:
Any suggestions would be greatly appreciated.
Thanks!
Upvotes: 1
Views: 2325
Reputation: 41
Just in case this could be helpful: I've seen this error appear in next case: Use a dictionary to keep weak references to BitmapData objects in use (for profiling purposes). Then, at some points in time, access the width and heigth properties of these BitmapData objects in that dictionary to calculate the total size of allocated BitmapData.
As elsewhere in the code, the bitmapData is dereferenced via: bitmapData.dispose(); bitmapData=null;
In the dictionary, the pointer still may not have been removed by the garbage collector. accessing the disposed bitmapData appears to give the mentioned error message.
Upvotes: 0
Reputation: 3522
This is most likely because your application is using too much memory and has hit a limit. Check Flash Player's memory usage.
A way to catch and deal with this error is to simply use a try..catch
block:
try {
var newBitmapData:BitmapData = oldBitmapData.clone();
} catch (e:Error) {
if (e.errorID == 2015) {
// handle it
} else {
throw e;
}
}
If you keep running into this error and can confirm that there is indeed a correlation with increased memory usage, then the one thing you want to do first is make sure that you are properly disposing of any discarded BitmapData
objects:
// dispose first
myBitmapData.dispose();
myBitmapData = null;
Before you lose all references to a BitmapData instance, you want to dispose()
it first.
Upvotes: 1
Reputation: 4434
did you try draw()
or copyPixels()
instead of clone()
? anyway grapefrukt is right about the try
block
Upvotes: 0
Reputation: 27045
Wrapping your clone code with a try {} catch (e:Error){}
seems like it would stop this from crasching your app.
I'd guess that the cause of the problem is running out of memory. Keeping a full clone of the canvas for each undo step gets rather expensive fast. The Invalid BitmapData
is most commonly caused by trying to create bitmaps larger than 2880px in either direction (fp9, fp10 goes a bit higher), but it wouldn't surprise me if this occurs in memory related problems too.
Upvotes: 0