-
Let's say, I disabled the GC, so now i have to free the dynamically allocated memory. But i want to free the memory only when the program is exited because that's how my program works. How would i know, that the program is exited so now i can free up the memory. |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 4 replies
-
You don't need free any memory/close file handle when program exit, the OS will do it for you |
Beta Was this translation helpful? Give feedback.
-
but is it preferred to do it that way? according to some guy on the internet :
is there any other good solution? |
Beta Was this translation helpful? Give feedback.
-
That's why the topic of this discussion is "How to check if a nelua program exited with code 0" Let's say i have a string which is being used by a function in -- function.nelua
## pragmas.nogc = true
require "string"
local mystring= string.create(5)
mystring= "\x1B[31m"
global function test()
print(mystring)
end -- main.nelua
test() I want to free the memory of the variable Now because i'have been thinking about it while writing this.. If that's the case then i have wasted codehz's time |
Beta Was this translation helpful? Give feedback.
-
you could use local str = string.create("test")
defer str:destroy() end
# whatever another method: use __close( search it in https://nelua.io/overview/ ) 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) |
Beta Was this translation helpful? Give feedback.
-
@codehz said valid points, and you could just disable the GC at compile-time and never deallocate. But I would also add that another option to avoid leaks when using valgrind like tools, is to disable the GC at runtime instead of compile-time, calling |
Beta Was this translation helpful? Give feedback.
you could use
defer
to make it called before function endsample:
another method: use __close( search it in https://nelua.io/overview/ )
example:
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)