# Memory leakage in usrp2::eth\_buffer

**URL:** <https://www.ruby-forum.com/t/memory-leakage-in-usrp2-eth-buffer/204320>\
**Category:** GNU Radio\
**Created:** [March 9, 2011, 7:37pm UTC](https://www.ruby-forum.com/t/memory-leakage-in-usrp2-eth-buffer/204320 "2011-03-09T19:37:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [March 9, 2011, 7:37pm UTC](https://www.ruby-forum.com/t/memory-leakage-in-usrp2-eth-buffer/204320/1 "2011-03-09T19:37:06Z")

</div>

Hi,  
in the method usrp2::eth\_buffer::open in case the attempt to use the  
socket option PACKET\_RX\_RING  
fails an mmap is performed instead of a malloc.

The method usrp2::eth\_buffer::close however has to perform a munmap in  
order to let the  
kernel release the mapped memory, indeed only closing the file  
descriptor isn’t enough.

I suggest to change (inside the close method):

```
if (!d_using_tpring && d_buf)
       free(d_buf);

```

to:

if (!d\_using\_tpring) {  
free(d\_buf);  
} else {  
if (d\_buf) munmap(d\_buf, d\_buflen);  
}

note that there is no need to test for d\_buf in case of free call  
indeed freeing a pointer to 0 is perfect valid code, the standard  
say that in case of free(NULL) no action is taken, to the other side  
munmap a NULL pointer is not permitted (hence the test).

Regards  
Gaetano M.

–

> **[c++ today](http://cpp-today.blogspot.com)**
>
> "Almost" day by day thoughts on code inspected

---

<div class="post-metadata">

**Author:** ![Gaetano\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/45deac/32.png) [@Gaetano\_M](https://www.ruby-forum.com/u/Gaetano_M)\
**Post date:** [March 10, 2011, 12:16pm UTC](https://www.ruby-forum.com/t/memory-leakage-in-usrp2-eth-buffer/204320/2 "2011-03-10T12:16:03Z")

</div>

This is the patch:

— gnuradio-3.3.0/usrp2/host/lib/eth\_buffer.cc 2010-06-01  
07:04:09.000000000 +0200  
+++ gnuradio-3.3.0-fix/usrp2/host/lib/eth\_buffer.cc 2011-03-10  
11:05:52.652353106 +0100  
@@ -156,8 +156,14 @@  
{  
// if we have background thread, stop it here

- if (!d\_using\_tpring && d\_buf)

- if (!d\_using\_tpring && d\_buf) {  
free(d\_buf);

- }

- else {

- 

```
   if(d_buf) {

```

- 

```
       munmap(d_buf, d_buflen);

```

- 

```
   }

```

- }

On Wed, Mar 9, 2011 at 7:35 PM, Gaetano M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ## I suggest to change (inside the close method): if (d\_buf) munmap(d\_buf, d\_buflen);
> 
> [cpp-today.blogspot.com](http://cpp-today.blogspot.com)

–  
[cpp-today.blogspot.com](http://cpp-today.blogspot.com)

---

<div class="post-metadata">

**Author:** ![Gaetano\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/45deac/32.png) [@Gaetano\_M](https://www.ruby-forum.com/u/Gaetano_M)\
**Post date:** [March 12, 2011, 3:32am UTC](https://www.ruby-forum.com/t/memory-leakage-in-usrp2-eth-buffer/204320/3 "2011-03-12T03:32:46Z")

</div>

Tom,  
let me know when you have applied it.

On Thu, Mar 10, 2011 at 2:07 PM, Tom R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > 07:04:09.000000000 +0200
> > 
> > - else {
> > 
> > > Hi,
> > > 
> > > }  
> > > [cpp-today.blogspot.com](http://cpp-today.blogspot.com)  
> > > [Discuss-gnuradio Info Page](http://lists.gnu.org/mailman/listinfo/discuss-gnuradio)

–

> **[c++ today](http://cpp-today.blogspot.com)**
>
> "Almost" day by day thoughts on code inspected
