Skip to content

add close method to explicitly free TagFiles#2

Open
Whoops wants to merge 1 commit intobickfordb:masterfrom
Whoops:master
Open

add close method to explicitly free TagFiles#2
Whoops wants to merge 1 commit intobickfordb:masterfrom
Whoops:master

Conversation

@Whoops
Copy link

@Whoops Whoops commented Apr 17, 2013

I've added a close method for tagfiles. Theres actually a couple reasons for this. One is simply to avoid leaking file handles in the event that the garbage collector doesn't free them up fast enough. I actually haven't been able to write a test case where this is a problem, but it's still good practice.

The other issue that I actually DID run afoul of, is it's currently easy to write code that, when compiled with ghc -O actually frees the TagFile before your done with it.
Consider the following snippet which inserts code into a acid-state database:

insertFile database file = do
  tagFile <- TL.open file
  mTag <- TL.tag $ fromJust tagFile
  let tag = fromJust mTag
  artist <- TL.artist tag
  album <- TL.album tag
  title <- TL.title tag
  genre <- TL.genre tag
  comment <- TL.comment tag
  track <- TL.track tag
  year <- TL.year tag
  update database (AddTrack file artist album title genre comment track year)
  return ()

When compiled with optimizations GHC actually realizes that tagFile is never used, and allows it to be GCed, the resulting error is:

pure virtual method called
terminate called without an active exception

adding an explicit

  close $ fromJust tagFile

to that example resolves it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant