two questions: bmpdata dispose and leaking

90 views
Skip to first unread message

Mark

unread,
Sep 23, 2014, 7:16:13 AM9/23/14
to haxe...@googlegroups.com
hi Guys,

Two questions here, if I may:
1. Is there a tool out there by which I can monitor my app to see if I leak memory? (on top of e.g. process monitoring, etc, so some "Haxe specific" tool I mean here. Or an analyzer maybe -bottlenecks, potential leaks, coding style, etc-)

2. Found something interesting and I don't know what causes the issue.
SHORT VERSION:
When calling localBitmapDataVar.dispose() of a -monster- object instance, all monsters are removed from stage, not just the one which called dispose on itself. Why?

LONG VERSION:
I have an array storing the instances of a Sprite-extending class. It's about spawning several from the same monster and keep track of them:
var myFoo : Array<MySpriteClass> = null;

When I add items to it, I do this:
myFoo.add(new MySpriteClass());
openfl
.Lib.current.stage.addChild(myFoo[lastitem]); //should add openfl.Lib... etc?

For remove, I do this:
myFoo.remove(objMySpriteClass); //where objMyS... is a reference to the monster object.
//As I did not have a better idea -yet-, my Game instance gets it back as a function param from the caller (the call is something like "MyParentGameObj.callback_function(this, otherparams);")

I have a destructor function in my 'MySpriteClass'. I call this function from 'Game' before I remove/"nullify" a particular monster-object (ie it gets shot, so monster is out of game).

The issue comes here. I destroy stuff in my function (local timer & event listeners), all goes well, except for one thing!
MyBitmapDataVar.dispose();
This variable stores the by-frame animation of the monster. I draw frames in stage update with TileSheet.drawTiles.
If I call the code line above, ALL my monsters disappear from the screen(stage), instead of the one which should.
* New monsters spawn as intended after (and before) this
* 'MyBitmapDataVar' is a class-level local (private), non-static variable
* The Asset / graphics is common of course, as monsters are -kinda- identical


My expectation was, all mobs (object instances) create their own BitmapData object and store "their own" animation frames, so if I dispose the one related to that particular object, that only that monster disappears.

What did I miss learning Haxe & openFL?

[FYI - If I remove the dispose() line, everything goes fine: one monster is removed, spawning goes on as intended, etc.]


Thank you for all the trouble in advace, and for reading this long post :)


Cheers,
Mark

David Elahee

unread,
Sep 23, 2014, 7:42:41 AM9/23/14
to haxe...@googlegroups.com
Hi !

Those are platform specific issues, namely flash ones.

1- Adobe Scout is your friend
2- disposing frees the pixel memory beyond the BitmapData which is just a pointer holder, so it make sense disposing will propaget over all bitmap referencing the pixels.

ps: protip : store your bitmapdata separately (static?level?) from your entity object and free them only if need be.
pps : see @deepnight/@ncannasse library/technique for loading data from their ludum dare, you'll learn what I mean by practice.

++

--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
---
You received this message because you are subscribed to the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/d/optout.



--
David Elahee


Mark

unread,
Sep 23, 2014, 7:54:12 AM9/23/14
to haxe...@googlegroups.com
hi David,

Thank you!
It's somewhat funny we have a variable lottery here (either pointer or not), especially with the error sinking mastery, but I can live with these, just learning into this business you know :P

If it is a pointer ofc I'll move the code out from ""local class"". Probably to a bitmapdata manager (hmm. with reference counters, perhaps? :).

Will look into the codes you mention in pps! An extra thanks may fly to your shoulder for that! ;)


Cheers,
Mark

Hugh

unread,
Sep 24, 2014, 1:03:48 AM9/24/14
to haxe...@googlegroups.com
Hi,
So in haxe (and all similar languages) what looks like an object, is actually a reference (pointer) to an object.  So all your monsters have their own "BitmapData" instance, but all these point to the same data.  So if you clear the contents of the bitmap (with dispose), you clear the contents of all your bitmaps.

The easy answer here is simply not to call dispose.  Just drop the reference to the bitmap (set you variable to null).  Or if you have removed the object from the stage and do not have any references to it (eg, in event handlers), you don't actually need to do anything else.  Just let the garbage collects clean it up when it feels like it.

Hugh

David Elahee

unread,
Sep 24, 2014, 3:23:46 AM9/24/14
to haxe...@googlegroups.com
Well in as3 you have to call dispose at one point otherwise this mem is wasted, hence why I do it in statics.

It seems completely incredible but I have witnesses ;)

--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
---
You received this message because you are subscribed to the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/d/optout.



--
David Elahee


Reply all
Reply to author
Forward
0 new messages