473,320 Members | 2,024 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

getifaddrs - Memory leak

158 100+
Hello Bytes,

I am using getifaddrs from ifadddrs.h on Debian Linux (Lenny).

Im using valgrind to check for memory leaks in my program. It seems that I have a memory leak in the code below but I cant figure out how to fix it. I thought i was freeing the address structure right but no luck.

If I do not run the loop and call "ifAddrStruct = ifAddrStruct->ifa_next;" I don't get a memory leak but it also destroys the functionality of my method.

Any thoughts?

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <ifaddrs.h> // Used for getting local address
  3.  
  4.     struct ifaddrs * ifAddrStruct = NULL;
  5.     getifaddrs(&ifAddrStruct);
  6.  
  7.     while (ifAddrStruct != NULL) {
  8.  
  9.         if (ifAddrStruct->ifa_addr->sa_family == AF_INET && strcmp(ifAddrStruct->ifa_name, "lo0") != 0) { // check it is IP4 and not lo0
  10.  
  11.         }
  12.         ifAddrStruct = ifAddrStruct->ifa_next;
  13.     }
  14.  
  15.     freeifaddrs(ifAddrStruct);
  16.     ifAddrStruct = NULL;
  17.  
  18.  
Jun 28 '10 #1

✓ answered by kardon33

Well i figured it out.

I had to create a second address structure like:

Expand|Select|Wrap|Line Numbers
  1. struct ifaddrs * ipa = NULL;
Then set ipa equal to ifAddrStruct before using ipa in the while loop.

This allows the code to keep a copy of linked list head ifAddrStruct for freeing.

Final Code looks like:

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3.     struct ifaddrs * ifAddrStruct, *ipa = NULL;
  4.     void * tmpAddrPtr = NULL;
  5.     char s[INET6_ADDRSTRLEN];
  6.  
  7.  
  8.     getifaddrs(&ifAddrStruct);
  9.     localAddress.clear();
  10.  
  11.     ipa =  ifAddrStruct;
  12.     while (ipa != NULL) {
  13.  
  14.         if (ipa->ifa_addr->sa_family == AF_INET && strcmp(ipa->ifa_name, "lo0") != 0) { // check it is IP4 and not lo0
  15.  
  16.  
  17.             // is a valid IP4 Address
  18.             tmpAddrPtr = &((struct sockaddr_in *) ipa->ifa_addr)->sin_addr;
  19.  
  20.             inet_ntop(AF_INET, tmpAddrPtr, s, sizeof (s));
  21.  
  22.  
  23.         }
  24.  
  25.          ipa = ipa->ifa_next;
  26.     }
  27.  
  28.     freeifaddrs(ifAddrStruct);
  29.  
  30.  

1 5000
kardon33
158 100+
Well i figured it out.

I had to create a second address structure like:

Expand|Select|Wrap|Line Numbers
  1. struct ifaddrs * ipa = NULL;
Then set ipa equal to ifAddrStruct before using ipa in the while loop.

This allows the code to keep a copy of linked list head ifAddrStruct for freeing.

Final Code looks like:

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3.     struct ifaddrs * ifAddrStruct, *ipa = NULL;
  4.     void * tmpAddrPtr = NULL;
  5.     char s[INET6_ADDRSTRLEN];
  6.  
  7.  
  8.     getifaddrs(&ifAddrStruct);
  9.     localAddress.clear();
  10.  
  11.     ipa =  ifAddrStruct;
  12.     while (ipa != NULL) {
  13.  
  14.         if (ipa->ifa_addr->sa_family == AF_INET && strcmp(ipa->ifa_name, "lo0") != 0) { // check it is IP4 and not lo0
  15.  
  16.  
  17.             // is a valid IP4 Address
  18.             tmpAddrPtr = &((struct sockaddr_in *) ipa->ifa_addr)->sin_addr;
  19.  
  20.             inet_ntop(AF_INET, tmpAddrPtr, s, sizeof (s));
  21.  
  22.  
  23.         }
  24.  
  25.          ipa = ipa->ifa_next;
  26.     }
  27.  
  28.     freeifaddrs(ifAddrStruct);
  29.  
  30.  
Jun 28 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Debian User | last post by:
Hi, I'm trying to discover a memory leak on a program of mine. I've taken several approaches, but the leak still resists to appear. First of all, I've tried to use the garbage collector to...
2
by: Elbert Lev | last post by:
#When I'm running this script on my windows NT4.0 box, #every time dialog box is reopened there is memory growth 384K. #Bellow is the text I sent to Stephen Ferg (author of easygui) # I have...
3
by: Jeremy Lemaire | last post by:
Hello, I am working on cross platform code that is displaying a huge memory leak when compiled on 11.00 HPUX using the aCC -AA flag. It is not leaking on NT, LINUX, Solaris, or HPUX without the...
32
by: John | last post by:
Hi all: When I run my code, I find that the memory that the code uses keeps increasing. I have a PC with 2G RAM running Debian linux. The code consumes 1.5G memory by the time it finishes...
8
by: ranjeet.gupta | last post by:
Dear All Is the Root Cause of the Memory corruption is the Memory leak, ?? suppose If in the code there is Memory leak, Do this may lead to the Memory Corruption while executing the program ? ...
17
by: José Joye | last post by:
Hi, I have implemented a Service that is responsible for getting messages from a MS MQ located on a remote machine. I'm getting memory leak from time to time (???). In some situation, it is...
4
by: Don Nell | last post by:
Hello Why is there a memory leak when this code is executed. for(;;) { ManagementScope scope = new ManagementScope(); scope.Options.Username="username"; scope.Options.Password="password";...
23
by: James | last post by:
The following code will create memory leaks!!! using System; using System.Diagnostics; using System.Data; using System.Data.SqlClient; namespace MemoryLeak
7
by: Ragnar Agustsson | last post by:
Hi all I have been wandering about the best way to sandbox memory leaks in 3rd party libraries when using them from the .Net framework. I have a 3rd party library, written in C++, that leaks a...
22
by: Peter | last post by:
I am using VS2008. I have a Windows Service application which creates Crystal Reports. This is a multi theaded application which can run several reports at one time. My problem - there is a...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.