Skip to content

Commit

Permalink
Fixing memory leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Sullivan committed Oct 1, 2020
1 parent 3da7ed6 commit 2623deb
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Example-iOS/Source/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ struct ContentView: View {

struct MyRive: UIViewControllerRepresentable {
func makeUIViewController(context: Context) -> RiveViewController {
return RiveViewController(resource: "sheep", withExtension: "riv")
return RiveViewController(resource: "truck", withExtension: "riv")
}

func updateUIViewController(_ uiViewController: RiveViewController, context: Context) {}
Expand Down
4 changes: 4 additions & 0 deletions Source/Rive.mm
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ -(instancetype) initWithContext:(CGContextRef) context {
}
}

-(void) dealloc {
delete _renderer;
}

-(void) alignWithRect:(CGRect)rect withContentRect:(CGRect)contentRect withAlignment:(Alignment)alignment withFit:(Fit)fit {
// NSLog(@"Rect in align %@", NSStringFromCGRect(rect));

Expand Down
16 changes: 14 additions & 2 deletions Source/RiveRenderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
namespace rive
{

/*
* RenderPaint
*/

enum class RiveGradient
{
None,
Expand Down Expand Up @@ -68,7 +72,6 @@ namespace rive
Luminosity = static_cast<int>(BlendMode::luminosity)
};


class RiveRenderPaint : public RenderPaint
{
private:
Expand Down Expand Up @@ -103,6 +106,10 @@ namespace rive
void completeGradient() override;
};

/*
* RenderPath
*/

enum class RivePathCommandType
{
MoveTo,
Expand Down Expand Up @@ -145,14 +152,19 @@ namespace rive
void close() override;
};

/*
* Renderer
*/

class RiveRenderer : public Renderer
{
private:
CGContextRef ctx;

public:
RiveRenderer(CGContextRef context) : ctx(context) {}

~RiveRenderer();

void save() override;
void restore() override;
void transform(const Mat2D &transform) override;
Expand Down
17 changes: 10 additions & 7 deletions Source/RiveRenderer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
}

RiveRenderPaint::~RiveRenderPaint() {
NSLog(@"Releasing paint resources");
// NSLog(@"Releasing paint resources");
CGColorRelease(cgColor);
CGGradientRelease(gradient);
}
Expand All @@ -49,6 +49,7 @@
((float)(value & 0xFF))/0xFF,
((float)((value & 0xFF000000) >> 24))/0xFF
};
CGColorRelease(cgColor);
cgColor = CGColorCreate(baseSpace, color);
}

Expand Down Expand Up @@ -193,7 +194,7 @@
}

RiveRenderPath::~RiveRenderPath() {
NSLog(@"Releasing path resources");
// NSLog(@"Releasing path resources");
CGPathRelease(path);
}

Expand Down Expand Up @@ -244,16 +245,19 @@
CGPathAddCurveToPoint(path, NULL, ox, oy, ix, iy, x, y);
}

// Renderer
/*
* Renderer
*/

RiveRenderer::~RiveRenderer() {
// NSLog(@"Releasing renderer c++");
}

// Implement save by creating a sublayer
void RiveRenderer::save() {
// NSLog(@" --- Renderer::save");
CGContextSaveGState(ctx);
}

// Implement restore by moving back up to the parent layer. If the parent
// layer is ctx, then there's been a restore without a save
void RiveRenderer::restore() {
// NSLog(@" -- Renderer::restore");
CGContextRestoreGState(ctx);
Expand Down Expand Up @@ -435,6 +439,5 @@

namespace rive {
RenderPaint* makeRenderPaint() { return new RiveRenderPaint(); }

RenderPath* makeRenderPath() { return new RiveRenderPath(); }
}
2 changes: 1 addition & 1 deletion submodules/rive-cpp

0 comments on commit 2623deb

Please sign in to comment.