Re: [gnet] gnet_udp_packet_receive question



On Sun, 5 Jan 2003, Tim Muller wrote:
> On Sunday 05 January 2003 19:51, Axel Bock wrote:
> > I have some trouble with this one: from the APIdoc
> >
> > gnet_udp_packet_receive_new ()
> > ... data is a shallow copy and must be deallocated by the caller if
> > necessary when appropriate.
> >
> > what exactly does this mean? *must* be deallocated *if neccessary*? ah.
> > makes quite sense :-)
> > well, as far as I know is a shallow copy (in this case) a pointer to
> > some static memory within the original function, and not some
> > malloc()'ed memory. so this doesnt have to be freed, does it?
>
> As far as I understand it, _you_ allocate the buffer 'data', and the function
> gnet_udp_packet_receive_new() just uses this information to make a GUdpPacket
> structure, so in the end it is up to you to free the buffer when you are done
> with the packet that you have received. If you need to free it or not depends
> on whether you allocated the memory dynamically of course.
>
> Here is some code snippet that works for me, maybe you find it helpful (I know
> this is not brilliant code, but it seems to work so far; the code scenario
> here is a client that uses a random UDP socket to send out requests to
> servers and then receives the replies on that same socket. I've left out the
> code where packets are sent).

Tim is right.  It makes a shallow copy.  I wrote "if necessary" because
the memory may be a string constant, which you wouldn't want to delete.
But I admit, it's confusing.

In fact, I was working on the UDP API for 1.3 and I will probably get rid
of the UdpPacket structure altogether, since it has two different uses and
so is confusing.  Instead, you will just pass buffers to send and receive.
I hope to have this done today.  I made changes to the constructors a few
weeks ago.


> PS: List, I hope it's alright to send code snippets. I don't think this one is
> too long, but please feel free to tell me off if this is against list policy
> :-)

Short code snippets like this are welcome.

David

---
      __          _    __ David Helder - dhelder umich edu
  ___/ /__ __  __(_)__/ / <http://www.eecs.umich.edu/~dhelder>
 / _  / _ `/ |/ / / _  /  DiaWebLog: <http://www.diaweblog.org>
 |_,_/|_,_/|___/_/|_,_/   Paper CD Case: <http://www.papercdcase.com>




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