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: calling free() on an object does not free all referenced objects. #578

Closed
MarcelKlammer opened this issue Jan 24, 2023 · 5 comments

Comments

@MarcelKlammer
Copy link
Contributor

Freeing up memory does not seem to work for referenced objects. The memory is slowing increasing, eg. on SundaeSwap, which is calling getUtxos() frequently, keeping a wallet connected results in the wasm memory slowly increasing to 2GB and then crashing.

  for(let i = 0; i < 500000; i++) {

    const bigNum              = CSL.BigNum.from_str('1000000')
    const value               = CSL.Value.new(bigNum)

    // To be clear, value.free() should be freeing all referenced objects, like coin and all multiassets.

    value.coin().free()       // This call does nothing: memory is still increasing
    value.free()              // Frees only CSL.Value properties?

    bigNum.free()             // Adding this call keeps: wasm.memory.buffer.byteLength steady.
  }

@MarcelKlammer
Copy link
Contributor Author

I narrowed it down to:

CSL.MultiAsset.insert()
CSL.Assets.insert()

Even keeping the return values in an array to later free them, memory increases.

@lisicky
Copy link
Contributor

lisicky commented Jan 24, 2023

Hi @MarcelKlammer! In the CSL, if you use an object as an argument, the CSL does not capture the object reference but copies the object's value for internal usage. We do that because we can't control a lifetime of external objects inside our rust codebase.

CSL.Value.new(bigNum)

In your example, when you put bigNum as a CSL's function argument, the function just uses a copy of that bigNum value.
And when some CSL's function returns an object, it's not an object that CSL's struct has inside, it's a copy of that object. That's why that code bellow doesn't free the original bigNum variable.

value.coin().free()

Till we don't add auto object freeing, you can use that workaround #542 (comment)

@MarcelKlammer
Copy link
Contributor Author

Ok, just to be 100% clear, all returned values of all functions are copies / new objects. And all parameters provided to CSL functions will be copied instead of referenced?

@lisicky
Copy link
Contributor

lisicky commented Jan 25, 2023

Unfortunately yes

@MarcelKlammer
Copy link
Contributor Author

Thanks for the clarification.

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

2 participants