Re: [Vala] Soup.Buffer memory



On Tue, 2013-09-24 at 14:24 +0200, andrea zambon wrote:
I am developing a service http with libsoup, but I have a memory problem.
When you make a request, the service memory increases during transmission
and decreases only after the closure of the service. Another call and will
increase again.

See the API docs for Soup.MessageBody.set_accumulate:
http://valadoc.org/libsoup-2.4/Soup.MessageBody.set_accumulate.html

I have no idea if it will work for encodings other than chunked, but I
don't see a reason why you would want to close the connection after
every request, so why not just use chunked?

The client is in another machine and the data may be very long (2 GB).
I try to user Soup.MemoryUse.TAKE but I get garbage data.
The Soup.Buffer is not free when sending a new buffer?

Using the default Soup.Buffer constructor with Soup.MemoryUse.TAKE is
very difficult to get right.  That flag is telling libsoup:

        "The caller has allocated the memory for the SoupBuffer's use;
        libsoup will assume ownership of it and free it (with g_free())
        when it is done with it."

However, Vala isn't actually aware of the interaction between this
parameter and libsoup's memory management.  According to the bindings,
the argument does not transfer ownership, which means Vala will free the
buffer for you, then it is likely to get reallocated somewhere else and
have its contents changed.  Furthermore, when libsoup tries to free it
your program will likely crash.

If you want to transfer ownership, you should use the Soup.Buffer.take
constructor.  By default valac will actually make a copy of the buffer
for you, so if you want to pass along your reference (which will
invalidate your local copy), use the (owned) keyword:

        var buffer = new Soup.Buffer.take ((owned) data);

I'm attaching a version of a server I wrote a while back which you may
want to take a look at.  It uses chunked encoding for larger files,
while smaller files are mmaped and use content length encoding.  It's
also pretty good about doing things asynchronously when possible.


-Evan

Attachment: simple-soup-server.vala
Description: Text Data



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]