I using ptr char for wrapping char*... (yay, libuv wrap)
However, should I use cstring? Is it recommend?
The string remains even after buffer is released on libuv.
I call alloc () and then cast it to ptr char, is this wrong?
I call alloc () and then cast it to ptr char, is this wrong?
it's ok, as long as you don't forget to call free()
or you can do something like this:
var x = newString(someLen)
call_some_c_func(x.cstring)
# and the gc will free the x for you
@jangko thx ! but, However, although I am freeing up the memory, when request coming shorter than the previous request, the character of the previous request is added at the end of the short request.
However, if call alloc0 instead, the problem is gone
for example,
GET /foo HTTP1.1\13\10
next req, (update: wrong req)
GET / HTTP1.1\13\10#1\13\10 ←this
# ↑↑↑
sometimes, adding garbage char such as unicode which a previous request does not have.
This was confirmed with the debug code here:
Since libuv calls the callback to store the buffer when storing the request, we are securing the memory here:
memory is released with one of the following:
when req is end,
when some error,
when parse error, (my parser returns a negative value on error.)
and, after callback end,
although memory leak does not occur even if high load is applied, but why is unexpected character added when alloc0() is not used ... ;;
as the name implies, alloc0 zeroed the chunk of memory requested, while alloc return raw memory block. when you free the memory, Nim internal allocator might reuse it at the next alloc call, if it thinks the previous block allocated could be reuse, hence the garbage.
if you think there is no need to do new allocation, you can keep the pointer around and reuse it, and call zeroMem if needed.
isn't this scenario a common practice when using C language?(I really have no idea about your C skills)
if you think alloc0 is slow because of the zeroMem overhead, you can put the zero terminator manually if you know the incoming packet length. (again, this is so common in C, no offense :) :) )
also I noticed that in line 67: if nread == 0: return, you don't free the buffer, doesn't it will leak?