# Is the SQLite3 gem known to have memory corruption?

**URL:** <https://www.ruby-forum.com/t/is-the-sqlite3-gem-known-to-have-memory-corruption/53442>\
**Category:** Ruby\
**Created:** [February 5, 2006, 6:51pm UTC](https://www.ruby-forum.com/t/is-the-sqlite3-gem-known-to-have-memory-corruption/53442 "2006-02-05T18:51:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Timothy\_JSWood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a8b319/32.png) [@Timothy\_JSWood](https://www.ruby-forum.com/u/Timothy_JSWood)\
**Post date:** [February 5, 2006, 6:51pm UTC](https://www.ruby-forum.com/t/is-the-sqlite3-gem-known-to-have-memory-corruption/53442/1 "2006-02-05T18:51:28Z")

</div>

Running the following produces bizarre results (trimmed down from  
an actual app):

* * *

#!/opt/local/bin/ruby

require ‘rubygems’  
require\_gem ‘sqlite3-ruby’

def each\_batch(size=1)  
until false  
batch = []  
(1…20).each {|i| batch \<\< “x”}  
yield batch  
end  
end

## database = SQLite3::Database.new("/tmp/test.db") counter = 0 each\_batch {|batch| database.transaction { counter += batch.length print “OK\n” } }

For a while it prints “OK”, but then eventually:

./test\_bad\_batch.rb:20: undefined method `length' for 4085314:Fixnum (NoMethodError) from /opt/local/lib/ruby/gems/1.8/gems/sqlite3-ruby-1.1.0/ lib/sqlite3/database.rb:593:in`transaction’  
from ./test\_bad\_batch.rb:19  
from ./test\_bad\_batch.rb:11:in `each_batch' from ./test_bad_batch.rb:11:in`each\_batch’  
from ./test\_bad\_batch.rb:18

That is the ‘batch’ is getting yielded as random pointer instead  
of the array generated inside each\_batch!

I’ve tried narrowing this further (particularly getting rid of  
SQLite usage), but this is the simplest case I can come up with so far.

I’m running on Mac OS X 10.4.4 with ruby 1.8.4 installed via  
Darwin Ports (as well as rubygems installed from there and then  
sqlite3 installed via that copy of gems).

* * *

% /opt/local/bin/ruby -v  
ruby 1.8.4 (2005-12-24) [powerpc-darwin8.3.0]

% /opt/local/bin/gem list sqlite3-ruby

\*\*\* LOCAL GEMS \*\*\*

## sqlite3-ruby (1.1.0) SQLite3/Ruby is a module to allow Ruby scripts to interface with a SQLite3 database.

I’ve tried both the system supplied sqlite (v3.1.3) and the latest  
version (installed via Darwin Ports), 3.3.3.

Any info would be helpful!

-tim

---

<div class="post-metadata">

**Author:** ![Timothy\_JSWood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a8b319/32.png) [@Timothy\_JSWood](https://www.ruby-forum.com/u/Timothy_JSWood)\
**Post date:** [February 5, 2006, 7:42pm UTC](https://www.ruby-forum.com/t/is-the-sqlite3-gem-known-to-have-memory-corruption/53442/2 "2006-02-05T19:42:49Z")

</div>

> Any info would be helpful!  
> See  
> [http://rubyforge.org/tracker/index.php?func=detail&aid=2453&group\_id=254&atid=1043](http://rubyforge.org/tracker/index.php?func=detail&aid=2453&group_id=254&atid=1043)  
> and the patch I submitted, which should fix the problem  
> [http://rubyforge.org/tracker/index.php?func=detail&aid=2955&group\_id=254&atid=1045](http://rubyforge.org/tracker/index.php?func=detail&aid=2955&group_id=254&atid=1045)

Regards,  
Sylvain

---

<div class="post-metadata">

**Author:** ![Timothy\_JSWood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a8b319/32.png) [@Timothy\_JSWood](https://www.ruby-forum.com/u/Timothy_JSWood)\
**Post date:** [February 5, 2006, 8:13pm UTC](https://www.ruby-forum.com/t/is-the-sqlite3-gem-known-to-have-memory-corruption/53442/3 "2006-02-05T20:13:03Z")

</div>

Thanks; I’ve applied this patch in my /opt/local/lib/ruby/gems/1.8/  
gems/sqlite3-ruby-1.1.0

It isn’t clear to me how to get the native code re-applied to the  
gem. After patching sqlite3\_api.i, I did:

% sudo ruby extconf.rb install sqlite3-ruby

but just reported there being nothing to do and re-running the  
test case yielded the same problem.

I’m sorry if this is a basic question, but I’m new to patching  
ruby itself. Any idea from the sqlite3 gem provider when this patch  
might be included?

-tim

---

<div class="post-metadata">

**Author:** ![Timothy\_JSWood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a8b319/32.png) [@Timothy\_JSWood](https://www.ruby-forum.com/u/Timothy_JSWood)\
**Post date:** [February 6, 2006, 6:24pm UTC](https://www.ruby-forum.com/t/is-the-sqlite3-gem-known-to-have-memory-corruption/53442/4 "2006-02-06T18:24:58Z")

</div>

Thanks; this patch did in fact fix my issue. Of course, it took  
me a while to figure out that I needed swig installed to get your  
patch to do anything. Luckily I noticed the SWIG bug in the tracker 🙂

-tim
