Skip to content

Commit

Permalink
Convert the image pointer to AutoPointer to avoid mem leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrik Stenmark committed Aug 4, 2009
1 parent bee817e commit 2dccb71
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 24 deletions.
5 changes: 5 additions & 0 deletions lib/gd2/cgd2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ def self.lib_path

# void* gdImagePngPtr(gdImagePtr im, int *size) (FUNCTION)
attach_function :gdImagePngPtr, [:pointer, :pointer], :pointer

# gdImageDestroy(gdImagePtr im) (FUNCTION)
attach_function :gdImageDestroy, [:pointer], :void

attach_function :gdFree, [:pointer], :void
end
14 changes: 12 additions & 2 deletions lib/gd2/image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@ class Image::IndexedColor < Image
end

class Image::TrueColor < Image
def release(ptr)
CGD2::gdImageDestroy(ptr)
end
def initialize(w, h)
@image_ptr = CGD2::gdImageCreateTrueColor(w, h)
@image_ptr = FFI::AutoPointer.new(
CGD2::gdImageCreateTrueColor(w, h),
self.method(:release)
)
end

def png
Expand All @@ -20,7 +26,11 @@ def png

size = size.get_int(0)

png_pointer.get_bytes(0, size)
png = png_pointer.get_bytes(0, size)

CGD2::gdFree(png_pointer)

png
end
end
end
5 changes: 4 additions & 1 deletion spec/image_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
require File.expand_path(File.join(File.dirname(__FILE__), "spec_helper"))

describe GD2::Image::TrueColor do
before do
CGD2.stub!(:gdFree)
end
it "should generate png string" do
image = GD2::Image::TrueColor.new(100, 100)
CGD2.should_receive(:gdImagePngPtr).and_return(mock("Pointer"))
CGD2.should_receive(:gdImagePngPtr).and_return(mock("Pointer", :get_bytes => ''))

image.png
end
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
require 'spec'

$: << 'lib'
require 'gd2'
require 'gd2-ffi'
31 changes: 11 additions & 20 deletions test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
$: << 'lib'
require 'gd2'

image_height = 100
image_width = 200

Expand Down Expand Up @@ -35,25 +34,17 @@
options[:offset_x] + options[:margin],
options[:offset_y] + options[:line_spacing] * (0 + 1))

10.times do |i|
image = GD2::Image::TrueColor.new(image_width * 2, image_height)
bg_rect.draw(image, white.to_i)
bg_rect_sprite.draw(image, yellow.to_i)

text = GD2::Canvas::Text.new(font, p_text, 0, line)
begin
text.draw(image, black)
rescue => e
puts e.message
end

image = GD2::Image::TrueColor.new(image_width * 2, image_height)
bg_rect.draw(image, white.to_i)
bg_rect_sprite.draw(image, yellow.to_i)

text = GD2::Canvas::Text.new(font, p_text, 0, line)
begin
text.draw(image, black)
rescue => e
puts e.message
end

GC.start

gets

#File.open("foo.png", "wb") do |file|
# file.write(image.png)
#end
File.open("foo.png", "wb") do |file|
file.write(image.png)
end

0 comments on commit 2dccb71

Please sign in to comment.