Memory Leak!!!!

This is a discussion on Memory Leak!!!! within the C++ Programming forums, part of the General Programming Boards category; Hi Friends, I have a question!!! Actually memory is allocated for a particular variable like this : Code: { struct ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,471
    And what is the question?

    Why to use malloc in C++?

    Could you use static allocation?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    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
    Portugal
    Posts
    7,412
    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.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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,189
    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.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  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
    Portugal
    Posts
    7,412
    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.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    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, 05: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21