Skip to content

Commit

Permalink
prevent point/rect leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
Geokureli committed Apr 9, 2024
1 parent 5b812e2 commit 604cf11
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 30 deletions.
4 changes: 2 additions & 2 deletions flixel/graphics/FlxGraphic.hx
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ class FlxGraphic implements IFlxDestroyable
key = null;
assetsKey = null;
assetsClass = null;
imageFrame = null; // no need to dispose _imageFrame since it exists in imageFrames
imageFrame = FlxDestroyUtil.destroy(imageFrame);

if (frameCollections == null) // no need to destroy frame collections if it's already null
return;
Expand Down Expand Up @@ -602,7 +602,7 @@ class FlxGraphic implements IFlxDestroyable
function get_imageFrame():FlxImageFrame
{
if (imageFrame == null)
imageFrame = FlxImageFrame.fromRectangle(this, FlxRect.get(0, 0, bitmap.width, bitmap.height));
imageFrame = FlxImageFrame.fromRectangle(this);

return imageFrame;
}
Expand Down
4 changes: 2 additions & 2 deletions flixel/graphics/frames/FlxFrame.hx
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ class FlxFrame implements IFlxDestroyable
return clippedFrame;
}

var clippedRect:FlxRect = FlxRect.get(0, 0).setSize(frame.width, frame.height);
final clippedRect:FlxRect = FlxRect.get(0, 0).setSize(frame.width, frame.height);
if (angle != FlxFrameAngle.ANGLE_0)
{
clippedRect.width = frame.height;
Expand All @@ -611,7 +611,7 @@ class FlxFrame implements IFlxDestroyable

clip.offset(-offset.x, -offset.y);
var frameRect:FlxRect = clippedRect.intersection(clip);
clippedRect = FlxDestroyUtil.put(clippedRect);
clippedRect.put();

if (frameRect.isEmpty)
{
Expand Down
4 changes: 3 additions & 1 deletion flixel/graphics/frames/FlxFramesCollection.hx
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,9 @@ class FlxFramesCollection implements IFlxDestroyable
*/
public function addSpriteSheetFrame(region:FlxRect):FlxFrame
{
var frame = new FlxFrame(parent);
// Ensure region not a weak rect
region = FlxRect.get().copyFrom(region);
final frame = new FlxFrame(parent);
frame.frame = checkFrame(region);
frame.sourceSize.set(region.width, region.height);
frame.offset.set(0, 0);
Expand Down
33 changes: 17 additions & 16 deletions flixel/graphics/frames/FlxImageFrame.hx
Original file line number Diff line number Diff line change
Expand Up @@ -95,21 +95,26 @@ class FlxImageFrame extends FlxFramesCollection
return null;

// find ImageFrame, if there is one already
var checkRegion:FlxRect = region;

if (checkRegion == null)
checkRegion = FlxRect.weak(0, 0, graphic.width, graphic.height);

var imageFrame:FlxImageFrame = FlxImageFrame.findFrame(graphic, checkRegion);
final checkRegion = FlxRect.get(0, 0, graphic.width, graphic.height);
if (region != null)
region.copyTo(checkRegion);

final imageFrame:FlxImageFrame = FlxImageFrame.findFrame(graphic, checkRegion);
checkRegion.put();
if (imageFrame != null)
{
if (region != null)
region.putWeak();

return imageFrame;
}

// or create it, if there is no such object
imageFrame = new FlxImageFrame(graphic);
final imageFrame = new FlxImageFrame(graphic);

if (region == null)
{
region = FlxRect.get(0, 0, graphic.width, graphic.height);
region = FlxRect.weak(0, 0, graphic.width, graphic.height);
}
else
{
Expand Down Expand Up @@ -192,14 +197,16 @@ class FlxImageFrame extends FlxFramesCollection
{
if (frameBorder == null)
frameBorder = FlxPoint.weak();

var imageFrames:Array<FlxImageFrame> = cast graphic.getFramesCollections(FlxFrameCollectionType.IMAGE);
for (imageFrame in imageFrames)
{
if (imageFrame.equals(frameRect, frameBorder) && imageFrame.frame.type != FlxFrameType.EMPTY)
return imageFrame;
}


frameBorder.putWeak();
frameRect.putWeak();
return null;
}

Expand Down Expand Up @@ -245,12 +252,6 @@ class FlxImageFrame extends FlxFramesCollection
return imageFrame;
}

override public function destroy():Void
{
super.destroy();
FlxDestroyUtil.destroy(frame);
}

function get_frame():FlxFrame
{
return frames[0];
Expand Down
2 changes: 1 addition & 1 deletion flixel/tile/FlxBaseTilemap.hx
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ class FlxBaseTilemap<Tile:FlxObject> extends FlxObject
*/
public function calcRayEntry(start, end, ?result)
{
var bounds = getBounds();
var bounds = getBounds(FlxRect.weak());
// subtract 1 from size otherwise `getTileIndexByCoords` will have weird edge cases (literally)
bounds.width--;
bounds.height--;
Expand Down
17 changes: 9 additions & 8 deletions flixel/util/FlxPool.hx
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,21 @@ class FlxPool<T:IFlxDestroyable> implements IFlxPool<T>
var i:Int = _pool.indexOf(obj);
// if the object's spot in the pool was overwritten, or if it's at or past _count (in the inaccessible zone)
if (i == -1 || i >= _count)
{
obj.destroy();
_pool[_count++] = obj;
}
putHelper(obj);
}
}

public function putUnsafe(obj:T):Void
{
// TODO: remove null check and make private?
if (obj != null)
{
obj.destroy();
_pool[_count++] = obj;
}
putHelper(obj);
}

function putHelper(obj:T)
{
obj.destroy();
_pool[_count++] = obj;
}

public function preAllocate(numObjects:Int):Void
Expand Down

0 comments on commit 604cf11

Please sign in to comment.