Skip to content

How to free memory of a variable when the variable is used in a function #137

Answered by codehz
ghost asked this question in Q&A
Discussion options

You must be logged in to vote

you could use defer to make it called before function end
sample:

local str = string.create("test")
defer str:destroy() end
# whatever

another method: use __close( search it in https://nelua.io/overview/ )
example:

local str <close> = string.create("test")
# ...

warn: if you want to return the owned string, take care about it, you cannot destroy it before return, and you don't want to leak it if exception was throw (aka forgot to return the string in middle)

Replies: 5 comments 4 replies

Comment options

You must be logged in to vote
2 replies
@ghost
Comment options

@codehz
Comment options

Comment options

You must be logged in to vote
1 reply
@codehz
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected
Comment options

You must be logged in to vote
1 reply
@ghost
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants