Skip to content

Commit

Permalink
fix memory leak on cursor iteration (#24). (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipenoris committed Mar 10, 2019
1 parent b2e2857 commit 76f4b7d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
26 changes: 26 additions & 0 deletions benchmarks/memory_leak_check.jl
@@ -0,0 +1,26 @@

#
# Stress test to check for memory leaks (#24)
#

using Mongoc

client = Mongoc.Client() # default locahost:27017
db = client["testDB"]
collection=db["testCollection"]

document = Mongoc.BSON()
document["name"] = "Felipe"
document["age"] = 35
document["preferences"] = [ "Music", "Computer", "Photography" ]

# generate some entries
for i in 1:100000
push!(collection, document)
end

# read bson documents from collection
for i in 1:10000
for entry in collection
end
end
9 changes: 3 additions & 6 deletions src/api.jl
Expand Up @@ -336,16 +336,13 @@ end
#

function _iterate(cursor::Cursor, state::Nothing=nothing)
next = BSON()
handle = next.handle
handle_ref = Ref{Ptr{Cvoid}}(handle)
has_next = mongoc_cursor_next(cursor.handle, handle_ref)
next.handle = handle_ref[]
bson_handle_ref = Ref{Ptr{Cvoid}}()
has_next = mongoc_cursor_next(cursor.handle, bson_handle_ref)

if has_next
# The bson document is valid only until the next call to mongoc_cursor_next.
# So we should return a deepcopy.
return deepcopy(next), nothing
return deepcopy(BSON(bson_handle_ref[], enable_finalizer=false)), nothing
else
err = BSONError()
if mongoc_cursor_error(cursor.handle, err)
Expand Down

0 comments on commit 76f4b7d

Please sign in to comment.