×

Discussion Board

Results 1 to 7 of 7
  1. #1
    Super Contributor
    Join Date
    Sep 2009
    Posts
    732

    How to use RFile and RFs inside a function so that there is no memory leak?

    Hi everyone,

    I want to do some file operation in a file i know how to do that but it causes memory leak i want to know how to use RFs and RFile inside a function so that there should not me any memory leak my code is
    Code:
    void CHttpClient::GetPostBodyManuallyL()
    	{
             RFile file ;
             RBuf8 DataBuffer ; // this is having some data but i didnt mention here which i write in file
             RFs fsSession ;
             User::LeaveIfError(fsSession.Connect()) ;
             file.Open(fsSession,_L("C:\Data.txt"),EFileShareAny|EFileWrite|EFileRead) ;
             file.Write(DataBuffer) ;
            file.Close();
            fsSession.Close ;
          }
    I am calling this function thousands of times means in every 10 sec once. how to avaoide memory leak in this code?
    Thanks & Regards
    Amit Kumar Mangal
    we can help each other and progress together
    http://amitmangal.blogspot.com/

  2. #2
    Super Contributor
    Join Date
    Mar 2004
    Location
    Singapore
    Posts
    9,968

    Re: How to use RFile and RFs inside a function so that there is no memory leak?

    might be due to RBuf8 DataBuffer ; all depends on how you fill it and close the RBuf8

  3. #3
    Super Contributor
    Join Date
    Sep 2009
    Posts
    732

    Re: How to use RFile and RFs inside a function so that there is no memory leak?

    RBuf8 is i am using after inserting cleanup stack like this
    DataBuffer.CleanupClosePushL();
    DataBuffer.Create(DataSize) ; and after using this CleanupStack::PopAndDestroy(); i dont think this is giving error. If it is giving error please tell me how to solve this.

    What i think i need to put RFs and RFile also on cleanup stack. Am i right ?
    if i am right please tell me how to do that ?
    Thanks & Regards
    Amit Kumar Mangal
    we can help each other and progress together
    http://amitmangal.blogspot.com/

  4. #4
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,697

    Re: How to use RFile and RFs inside a function so that there is no memory leak?

    The snippet in #1 can not cause memory leaks, though it is not really prepared to handle error situations.
    The snippet in #3 is not that bad neither, though it mixes the Cleanup Stack into the picture, and possible leaves. If a leave occurs there, the file and session may get leaked, depending on how your method really looks like (RBuf8 handling happens before everything else, or in the middle of the code).
    This is why this discussion does not make any sense, since it is about some theoretical code and not the real one.
    If you want to use the Cleanup Stack, you should use it everywhere (so CleanupClosePushL RFs and RFile too), and use LeaveIfError with RFile::Open, and use RBuf8::CreateL instead of Create.
    And it is not clear why you think there is a leak. If it is in the emulator and you get an ALLOC panic, use HookLogger and you will see where the leak is. If it is about UHEAP macros, note that they can not track extension of the Cleanup Stack. If you run out of memory on the device, the problem is probably somewhere else, or at least in some lines you have not shown yet.

    For the question about pushing the fs and file:
    Code:
    RFile file ;
    //         RBuf8 DataBuffer ; // this is having some data but i didnt mention here which i write in file
             RFs fsSession ;
             User::LeaveIfError(fsSession.Connect()) ;
             CleanupClosePushL(fsSession);
             User::LeaveIfError(file.Open(fsSession,_L("C:\Data.txt"),EFileShareAny|EFileWrite|EFileRead));
             CleanupClosePushL(file);
             User::LeaveIfError(file.Write(DataBuffer));
    //        file.Close();
    //        fsSession.Close ;
             CleanupStack::PopAndDestroy(2); // file, fsSession
    While typing this code, I have noticed the missing () from fsSession.Close. If it is really missing in your code, that can be the reason.

  5. #5
    Super Contributor
    Join Date
    Sep 2009
    Posts
    732

    Re: How to use RFile and RFs inside a function so that there is no memory leak?

    Hi Wizard_hu

    I have noticed the missing () from fsSession.Close
    fsSession.Close this is written in my code.

    thanks for reply i will apply the same and check what happens .I am struggling last 7 months to make my application stable but it always get closed after 2 or 3 days or behaves abnormally.

    with the help of all yours i made this application and i will make it stable.

    thanks to forum nokia and all memebers for support.

    thank you
    Thanks & Regards
    Amit Kumar Mangal
    we can help each other and progress together
    http://amitmangal.blogspot.com/

  6. #6
    Regular Contributor
    Join Date
    Apr 2005
    Posts
    274

    Re: How to use RFile and RFs inside a function so that there is no memory leak?

    hi,
    Use in the following way: Not the exact syntax though -

    RFs fs;
    fs.Connect();

    RFile file;
    file.Open(fs,filepath,mode);

    file.Read() / file.Write();

    file.Close();
    fs.Close();

    Regards,
    himanshu.

  7. #7
    Nokia Developer Champion
    Join Date
    Jul 2004
    Posts
    2,015

    Re: How to use RFile and RFs inside a function so that there is no memory leak?

    What if the call to fs.Connect() fails?
    What if the call to file.Open() fails?
    What if the call to Read()/Write() fails?

    If this is intended as template/example code, its lacking basic error handling.

    Quote Originally Posted by himsymbian View Post
    hi,
    Use in the following way: Not the exact syntax though -

    RFs fs;
    fs.Connect();

    RFile file;
    file.Open(fs,filepath,mode);

    file.Read() / file.Write();

    file.Close();
    fs.Close();

    Regards,
    himanshu.

Similar Threads

  1. Problem Carbide.c++ / RFile & RFs
    By nico in forum Carbide.c++ IDE and plug-ins (Closed)
    Replies: 3
    Last Post: 2008-11-06, 17:26
  2. Getting RFile from RFs
    By gloria799 in forum Symbian
    Replies: 8
    Last Post: 2007-11-19, 08:16
  3. Difference between Rfs and Rfile
    By newnb in forum Symbian
    Replies: 1
    Last Post: 2006-09-27, 04:27
  4. Replies: 10
    Last Post: 2006-09-19, 13:38
  5. memory leak on emulator = memory leak on device
    By manmli in forum Mobile Java General
    Replies: 2
    Last Post: 2006-08-05, 19:54

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×