Thread: Is there a memory leak in my code?

  1. #1
    Registered User
    Join Date
    Jul 2017
    Posts
    28

    Is there a memory leak in my code?

    I wrote this shared object library for an external piece of hardware running Linux. When I run my code in a main() function (not shared object library) on my hardware, it works. However, when I run it through my hardware's coding language, it does not work unless, I delay the calls between functions. Is there anything glaringly wrong with my code?

    Shared Library Header
    Code:
    // Header File for sharedTCPIPv6.c
    
     
     
    // FUNCTION DECLARATIONS
     
    // Server
    struct sockaddr_in6 serv_addr;
    
     
    extern int openServerJ(); // Done
    
    extern void getServerAddrJ(int sockfd, int portNum, struct sockaddr_in6 *serv_addr); // Done
    
    extern int serverBindListenJ(int sockfd, struct sockaddr_in6 *serv_addr); // Done
    extern int serverAcceptJ(int sockfd); // Done
    extern int recvServerJ(int newsockfd, char buffer[]); // Done
    
    extern int sendServerJ(int newsockfd, char buffer[]); // Done
    extern int closeServerJ(int sockfd, int newsockfd); // Done
    
    // Server End
    
     
     
     
    // Client
    
    struct sockaddr_in6 Cserv_addr;
    
     
    extern int openClientJ(); // Done
    
    extern int getClientAddrJ(char ChostAddress[], int portNum, struct sockaddr_in6 *Cserv_addr); // Done
    
    extern int connectClientJ(int sockfd, struct sockaddr_in6 *Cserv_addr); // Done
    
    extern int sendClientJ(int sockfd, char Cbuffer[]); // Done
    
    extern int recvClientJ(int sockfd, char Cbuffer[]); // Done
    
    extern int closeClientJ(int sockfd); // Done
    
    // Client End
    Shared Library
    Code:
    
    // SHARED LIBRARY HEADER INCLUDE
    #include "sharedTCPIPv6.h"
    
    
    // INCLUDES
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <string.h>
    
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netdb.h>
    
    #include <arpa/inet.h>
    
    
    
    // //////////////// //
    // SERVER FUNCTIONS //
    // //////////////// //
    
    
    // int openServerJ();
    int openServerJ() {
     // Sockets Layer Call: socket()
     int sockfd;
    
     sockfd = socket(AF_INET6, SOCK_STREAM, 0);
     return(sockfd); // -1 on error
    }
    // END
    
    
    
    // void getServerAddrJ(int sockfd, int portNum, struct sockaddr_in6 *serv_addr);
    
    void getServerAddrJ(int sockfd, int portNum, struct sockaddr_in6 *serv_addr) {
     // Parse Address
     //bzero((char *) &serv_addr, sizeof(serv_addr));
     serv_addr->sin6_flowinfo = 0;
     serv_addr->sin6_family = AF_INET6; // AF_INET6 = 10;
     serv_addr->sin6_addr = in6addr_any;
     serv_addr->sin6_port = htons(portNum); // Convert the port number to network byte order
    }
    // END
    
    
    
    // int serverBindListenJ(int sockfd, struct sockaddr_in6 *serv_addr);
    
    int serverBindListenJ(int sockfd, struct sockaddr_in6 *serv_addr) {
     // Sockets Layer Call: bind()
     int tmpVar;
    
     struct sockaddr_in6 serv_addr2 = *serv_addr;
    
     tmpVar = bind(sockfd, (struct sockaddr *) &serv_addr2, sizeof(serv_addr2));
    
    
     // Bind() Error Catch
     if (tmpVar < 0) {
      return(1);
     }
    
     // Sockets Layer Call: listen()
     tmpVar = listen(sockfd, 5);
    
     // Listen() Error Catch
     return(tmpVar);
    }
    // END
    
    
    
    // int serverAcceptJ();
    int serverAcceptJ(int sockfd) {
     // Sockets Layer Call: accept()
     int newsockfd;
     struct sockaddr_in6 cli_addr;
     socklen_t clilen;
    
     clilen = sizeof(cli_addr);
     newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
     return(newsockfd);
     // Sockets Layer Call: inet_ntop()
     //inet_ntop(AF_INET6, &(cli_addr.sin6_addr),client_addr_ipv6, 100);
    }
    // END
    
    
    
    // int recvServerJ(int newsockfd, char buffer[]);
    int recvServerJ(int newsockfd, char buffer[]) {
     int n;
     //memset(buffer, 0, 256);
    
     // Sockets Layer Call: recv()
     n = recv(newsockfd, buffer, 255, 0);
    
     return(n);
    }
    // END
    
    
    
    // int sendServerJ(int newsockfd, char buffer[])
    
    int sendServerJ(int newsockfd, char buffer[]) {
     int n;
    
     // Sockets Layer Call: send()
     n = send(newsockfd, buffer, strlen(buffer) + 1, 0);
    
     // Send() Error Catch
     return(n);
    }
    // END
    
    
    
    // void closeServerJ(int sockfd, int newsockfd);
    
    int closeServerJ(int sockfd, int newsockfd) {
     // Sockets Layer Call: close()
     int tempVar;
    
     tempVar = close(sockfd);
     if(tempVar < 0) {
      return(tempVar);
     }
     tempVar = close(newsockfd);
    
     return(tempVar);
    }
    // END
    
    
    
    
    // //////////////// //
    // CLIENT FUNCTIONS //
    // //////////////// //
    
    
    // int openClientJ(); 
    int openClientJ() {
     // Sockets Layer Call: socket()
     int sockfd;
    
     sockfd = socket(AF_INET6, SOCK_STREAM, 0);
     return(sockfd);
    }
    // END
    
    
    
    // int getClientAddrJ(int portnum, struct sockaddr_in6 *serv_addr); 
    
    int getClientAddrJ(char ChostAddress[], int portNum, struct sockaddr_in6 *Cserv_addr) {
     struct hostent *server;
    
     //char *phostAddress = ChostAddress; // Change later
     char *phostAddress = "::1";
    
     server = gethostbyname2(phostAddress, AF_INET6);
     // Gethostbyname2() Error Catch
     if (server == NULL) {
      return(-1);
     }
    
     //memset((char *) &serv_addr, 0, sizeof(serv_addr));
     Cserv_addr->sin6_flowinfo = 0;
     Cserv_addr->sin6_family = AF_INET6;
     memmove((char *) &Cserv_addr->sin6_addr.s6_addr, (char *) server->h_addr, server->h_length);
     Cserv_addr->sin6_port = htons(portNum);
    
     return(0);
    }
    // END
    
    
    
    // int connectClientJ(int sockfd, struct sockaddr_in6 *serv_addr); 
    
    int connectClientJ(int sockfd, struct sockaddr_in6 *Cserv_addr) {
     // Sockets Layer Call: connect()
     int tempVar;
    
     struct sockaddr_in6 Cserv_addr2 = *Cserv_addr;
     tempVar = connect(sockfd, (struct sockaddr *) &Cserv_addr2, sizeof(Cserv_addr2));
     return(tempVar);
    }
    // END
    
    
    
    // int sendClientJ(int sockfd, char buffer[]); 
    int sendClientJ(int sockfd, char Cbuffer[]) {
     // Sockets Layer Call: send()
     int n;
    
     n = send(sockfd, Cbuffer, strlen(Cbuffer) + 1, 0);
    
     return(n);
    }
    // END
    
    
    
    // int recvClientJ(int sockfd, char buffer[]); 
    
    int recvClientJ(int sockfd, char Cbuffer[]) {
     int n;
    
     // memset(buffer, 0, 256);
    
     // Sockets Layer Call: recv()
     n = recv(sockfd, Cbuffer, 255, 0);
    
     return(n);
    }
    // END
    
    
    
    // void closeClientJ(int sockfd); 
    int closeClientJ(int sockfd) {
     // Sockets Layer Call: close()
     int tempVar;
    
     tempVar = close(sockfd);
    
     return(tempVar);
    }
    // END
    Last edited by JHugh; 08-03-2017 at 10:55 AM. Reason: Formatted code better

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by JHugh View Post
    Is there anything glaringly wrong with my code?
    Yes.

  3. #3
    Registered User
    Join Date
    Jul 2017
    Posts
    28
    Quote Originally Posted by algorism View Post
    Yes.
    Care to elaborate?

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by JHugh View Post
    Care to elaborate?
    Not really, no.
    Although I would suggest that you read my answer to your last question.
    Simply replace the word "suboptimal" with "totally and utterly wrong".

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    There are no memory leaks, because you don't call malloc anywhere.

    Your senders need to look like this:
    Code:
    size_t remaining = strlen(Cbuffer) + 1;
    do {
      ssize_t n = send(sockfd,Cbuffer,remaining,0);
      if ( n > 0 ) {
        // measure forward progress through the buffer
        // don't assume a single send call will always send the whole buffer in one go.
        Cbuffer += n;
        remaining -= n;
      } else if ( n == 0 ) {
        // remote disconnected
        break;
      } else {
        // do error handling
        // watch out for EAGAIN or EWOULDBLOCK if the socket is non-blocking, as
        // these are soft errors
        break;   // or perhaps continue
      }
    } while ( remaining > 0 );
    Your receivers need to have buffer size as an additional parameter.

    What is the purpose of those mystery global variables in the header file?

    What is the purpose of this copy?
    > struct sockaddr_in6 serv_addr2 = *serv_addr;
    > tmpVar = bind(sockfd, (struct sockaddr *) &serv_addr2, sizeof(serv_addr2));
    In other words, what's wrong with
    tmpVar = bind(sockfd, (struct sockaddr *)serv_addr, sizeof(*serv_addr));
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Jul 2017
    Posts
    28
    Well, it works in main() so it can't be completely wrong.

    Guess I'll just have to figure it out by myself.

    Thanks for your answer in the other post. As you can tell, I'm not too familiar with structures and pointers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory Leak Detector Code Rating/Review
    By septical in forum C Programming
    Replies: 4
    Last Post: 11-03-2011, 01:12 AM
  2. Memory Leak in AppWizard-Generated Code
    By jrohde in forum Windows Programming
    Replies: 4
    Last Post: 05-19-2010, 04:24 PM
  3. This code results in a memory leak, right?
    By KBriggs in forum C Programming
    Replies: 8
    Last Post: 05-06-2010, 05:31 PM
  4. memory leak in the code?
    By George2 in forum C++ Programming
    Replies: 20
    Last Post: 01-13-2008, 06:50 AM
  5. Linking error with memory leak detection code. HELP!
    By Lister in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2003, 03:54 PM

Tags for this Thread