Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory Leak with $ Operator #5

Open
rbohl64 opened this issue Jul 22, 2021 · 3 comments
Open

Memory Leak with $ Operator #5

rbohl64 opened this issue Jul 22, 2021 · 3 comments

Comments

@rbohl64
Copy link

rbohl64 commented Jul 22, 2021

The following code snippet will leak memory with the default gc in Nim 1.4.8 and the latest development build. Araq believes this is an issue with the library rather than with Nim.

while true:
     var totStr = ""
     for item in seqOfDecimals:
         totStr &= $item
@HugoGranstrom
Copy link
Contributor

I guess it's because we aren't freeing the cstring returned by mpd_to_sci. Does Nim have anyway of doing it out of the box or do we need to wrap a free function from C?

@stefantalpalaru
Copy link

Use c_free(): https://github.com/nim-lang/Nim/blob/0c4582c66524d58a3c72827d9546a5c5f1c40286/lib/system/ansi_c.nim#L149

@rbohl64
Copy link
Author

rbohl64 commented Jul 25, 2021

I'm not yet familiar with using the nim c interop and building nimble packages, but is seems a simple patch such as below could resolve the leak. Could someone with more than my current expertise make a fix?

Thanks,

proc `$`*(s: DecimalType): string =
  ## Convert DecimalType to string
  let cs = mpd_to_sci(s[], 0)
  result = $cs
  c_free(cs)

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

No branches or pull requests

3 participants