Thread: Memory Leak!!!!

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    26

    Memory Leak!!!!

    Hi Friends,

    I have a question!!!

    Actually memory is allocated for a particular variable like this :
    Code:
    {
    
    struct InstanceDataStruct *instanceData;
    
       instanceData = (struct InstanceDataStruct*)malloc(sizeof InstanceDataStruct));
    
    //some codes are there
    //some codes are there
         .......
         .......
    
    try
       {
       	 pthread_mutex_lock(&mutex);
         setWSSecurity(siei_abus, sieiUserId, sieiPsw);
    	 instanceData->client = new ATT::SIEIClient(sieiWsdl, siei_abus);
    	 pthread_mutex_unlock(&mutex);
      }
          catch (IT_Bus::Exception& e) {
          setRemedyReturnStatus(
            status, AR_RETURN_ERROR, AR_ERROR_CONNECT, "Couldn't create client connection");
            pthread_mutex_unlock(&mutex);
             return AR_RETURN_ERROR;         // Error at this line,says memory is lost
    }
    Please help me out

    Thanks in Advance!!!

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    And what is the question?

    Why to use malloc in C++?

    Could you use static allocation?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You also need to:
    1) Stop mixing tabs and spaces, thus fixing the indentation.
    2) If 1) does not fix the indentation, fix it now.
    3) Show relevant code, preferably the minimal amount of code needed to represent the problem, because there's absolutely no way to determine why from your code right now.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    instanceData is not being deallocated before that return statement. That's one of the possible memory leaks.
    Then whoever wrote this code is allocating again for one of it's members in instanceData->client = new ATT::SIEIClient(sieiWsdl, siei_abus), that's your second memory leak since instanceData itself never gets deallocated.
    Last edited by Mario F.; 09-10-2009 at 08:36 AM.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Because you are allocating memory, but you never free it.

    change -
    Code:
    struct InstanceDataStruct *instanceData;
    
       instanceData = (struct InstanceDataStruct*)malloc(sizeof InstanceDataStruct));
    to

    Code:
    struct InstanceDataStruct  instanceData;
    and the problem should go away if its related to the malloc. Without more specific information that's all i can suggest.
    Last edited by abachler; 09-10-2009 at 11:08 AM.

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    26
    Thanks a lot friend!!!!!

  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    26
    Thanks a lot friend!!!!

  8. #8
    Registered User
    Join Date
    Sep 2009
    Posts
    26

    Smile Memory Leak!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!11

    HI Mario F.,

    I have 2 catch statements within which 2 return statements are there.So should i need deallocate memory twice for your reference :

    Code:
    
    
    struct InstanceDataStruct *instanceData;
    
       instanceData = (struct InstanceDataStruct*)malloc(sizeof InstanceDataStruct));
    
    //some codes are there
    //some codes are there
         .......
         .......
    
    try
       {
       	 pthread_mutex_lock(&mutex);
         setWSSecurity(siei_abus, sieiUserId, sieiPsw);
    	 instanceData->client = new ATT::SIEIClient(sieiWsdl, siei_abus);
    	 pthread_mutex_unlock(&mutex);
      }
    
    
    
          catch (IT_Bus::Exception& e) {
             setStatus("ARPluginCreateInstance:", status, (char *) e.message());
             pthread_mutex_unlock(&mutex);
             return AR_RETURN_ERROR;           // 1 return statement here
          }
          catch (...) {
             pthread_mutex_unlock(&mutex);
             return AR_RETURN_ERROR;           //2 return statement here
          }

  9. #9
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Certainly. Each catch block offers a distinct execution path.

    Assuming there is nothing else going on with the code that you aren't showing, coming those catch blocks, you'll need to deallocate before each return statement. You will not be deallocating twice. You'd still be deallocating just once. Once for every execution path.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I'd suggest you get familiar with RAII. It will save you trouble especially with these kinds of problems.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Sep 2009
    Posts
    26

    Thumbs up

    Thanks Mario F,

    Your suggesion really helps me..

    Thanks a lot..

    Subhashish

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory leak in this case?
    By George2 in forum C++ Programming
    Replies: 3
    Last Post: 03-22-2008, 05:05 AM
  2. memory leak in the code?
    By George2 in forum C++ Programming
    Replies: 20
    Last Post: 01-13-2008, 06:50 AM
  3. Is this code memory leak free? ---> POSIX Threads
    By avalanche333 in forum C++ Programming
    Replies: 9
    Last Post: 04-13-2007, 03:19 PM
  4. Any Memory Leak Checking Tool?
    By George2 in forum C Programming
    Replies: 4
    Last Post: 06-21-2006, 11:02 PM
  5. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM