There are two static VALUE variables used in code for Shape#inspect method and in function used in code for Shape#part_start. In current version both methods causes core dumps when used several times. I suppose that they are not bound to symbol so they are deallocated by garbage collector soon after initialization.
Here is code for Shape#inspect bug reproduction
---
require 'shapelib'
ShapeLib::ShapeFile::open(ARGV[0]) do
|sf|
$stderr.puts('inspect test')
sf.each do
|s|
a=s.inspect
end
end
---
Reproduction of bug in Shape#part_start is somewhat more complicated
---
require 'shapelib'
def loops(parts, xvals, yvals)
o=[]
if parts
parts.each_index do
|idx|
o << []
(parts[idx]..(parts[idx+1] || (xvals.size-1))).each do
|i|
o.last << [xvals[i],yvals[i]]
end
end
end
o
end
if __FILE__==$0
ShapeLib::ShapeFile::open(ARGV[0]) do
|sf|
$stderr.puts('part_start test')
1000000.times do
$stderr.puts("\nshape start")
sf.each do
|s|
bd = loops(s.part_start, s.xvals, s.yvals)
pg=ShapeLib::Polygon.new(*bd)
end
end
end
end
---
I attach patch which corrects this problems.
--
Best regards
RNicz
Strip 'static' from VALUE variables
Logged In: YES
user_id=891257
Originator: NO
This patch also works, and avoids calling rb_str_new2 repeatedly
so performance should not be impacted.
<pre>
--- a/valconv.h
+++ b/valconv.h
@@ -298,6 +298,7 @@ static VALUE IntUnpack(int *up, unsigned num)
if (unpackfmt == 0) {
unpackid = rb_intern("unpack");
unpackfmt = rb_str_new2("i*");
+ rb_global_variable(&unpackfmt);
}
buf = rb_str_new((char *)up, sizeof(int) * num);
return rb_funcall(buf, unpackid, 1, unpackfmt);
</pre>
Are you sure that fixes it? I've made that change locally and I still get the error on MacOS X
Sorry, I don't have an account but can be reached at mmangino@elevatedrails.com
I also have a version of this code at http://github.com/mmangino/shapelib/tree/master
Mike