Discussion Board

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

    application get closed when opening file and file doesnt exist

    Hi Everyone,
    I am trying to open a file if file exist it works fine but if file doesnt exist my application get closed please tell me where is the error in this line and gow to remove that.
    Code:
    RFile DeleteFile ;
        RFs DeleteSession ;
       
        User::LeaveIfError(DeleteSession.Connect()) ;
        CleanupClosePushL(DeleteSession) ;
        TInt ErrorFile = User::LeaveIfError(DeleteFile.Open(DeleteSession,(_L("C:\\Data.txt")),EFileShareAny|EFileRead|EFileWrite)) ;//application get closed in this line if file doesnt exist.
        CleanupClosePushL(DeleteFile) ;
        if(ErrorFile==KErrNotFound)
             {
             DeleteFile.Create(DeleteSession,(_L("C:\\Data.txt")),EFileShareAny|EFileRead|EFileWrite) ;
             DeleteFile.Write(KStatusData) ;
             DeleteFile.Flush();
             DeleteFile.Close() ;
             
        	 }
    Last edited by amit.mangal; 2010-08-24 at 11:46.
    Thanks & Regards
    Amit Kumar Mangal
    we can help each other and progress together
    http://amitmangal.blogspot.com/

  2. #2
    Nokia Developer Moderator
    Join Date
    Mar 2003
    Location
    Lempäälä/Finland
    Posts
    29,183

    Re: application get closed when opening file and file doesnt exist

    Check the panic code: http://wiki.forum.nokia.com/index.ph...ded_panic_code

    Also do remove User::LeaveIfError() parts that you do not understand, they do cause leaves if there are errors.

  3. #3
    Nokia Developer Champion
    Join Date
    Jun 2008
    Location
    Gurgaon,India
    Posts
    4,112

    Re: application get closed when opening file and file doesnt exist

    Of course your application will get closed.Have a look in the sdk that what does the statement "User::LeaveIfError" means.

    And why don't put up a check on the existence of file before opening it.:

    if(BaflUtils::FileExists(DeleteSession,Filename))
    {
    ///// do what you want with the file.
    }

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

    Re: application get closed when opening file and file doesnt exist

    should i not use User::LeaveIfError() if i am not using this then how will i avoid memory leak my application behaving arbitary after 2 -3 days so i am again coding and taking care each line cause i am not able to find memory leak last 7 months.
    thankyou
    Thanks & Regards
    Amit Kumar Mangal
    we can help each other and progress together
    http://amitmangal.blogspot.com/

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    8

    Re: application get closed when opening file and file doesnt exist

    Hi

    U can use User::LeaveIfError() .

    In this case call your function in a TRAP or TRAPD and if there is a leave collect the appropriate error code(like in u r case file doesn't exist) and take necessary action.
    This way u r application will not quit.


    Thanks

  6. #6
    Nokia Developer Champion
    Join Date
    Jun 2008
    Location
    Gurgaon,India
    Posts
    4,112

    Re: application get closed when opening file and file doesnt exist

    User::LeaveIfError() has nothing to do with finding the cause of memory leak.Its just a way to avoid the execution of the code if there occurs an error in the line in which this statement is implemented.

    & in this case of file handling you can go with the way i suggested in post # 3

  7. #7
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,792

    Re: application get closed when opening file and file doesnt exist

    Since you want to check the result code a few lines later, you should not use User::LeaveIfError in this particular case.

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

    Re: application get closed when opening file and file doesnt exist

    Hi Varun
    thanks for reply i want to know that what is Trap and Trapd and how can we use do you have some link and i want to know if i dont use User::Leaveiferror is there any chance to memory leave i have to run my application 24 hours never get closed so how can i manage my R classes to avaoid memory leak
    thankyou
    Thanks & Regards
    Amit Kumar Mangal
    we can help each other and progress together
    http://amitmangal.blogspot.com/

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

    Re: application get closed when opening file and file doesnt exist

    hi wizard_hu

    this method suggested by you for opening a file please see this thread
    http://discussion.forum.nokia.com/fo...ht=#post766838
    Thanks & Regards
    Amit Kumar Mangal
    we can help each other and progress together
    http://amitmangal.blogspot.com/

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

    Re: application get closed when opening file and file doesnt exist

    Hi Vineet jain
    user::leaveiferror not related with any kind of memory leak?
    if this is true then i should search any other thing to find out memory leak in my code ?
    Thanks & Regards
    Amit Kumar Mangal
    we can help each other and progress together
    http://amitmangal.blogspot.com/

  11. #11
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,792

    Re: application get closed when opening file and file doesnt exist

    Do not worry, the code in the other thread is still fine. Just it has a single open attempt, while your new code attempts to open first, and create if necessary. So KErrNotFound has to be handled separately.
    For example
    Code:
    TInt ErrorFile = DeleteFile.Open(DeleteSession,(_L("C:\\Data.txt")),EFileShareAny|EFileRead|EFileWrite) ;
    if (ErrorFile!=KErrNotFound) User::LeaveIfError(ErrorFile);
    would do that. There are far better solutions, just it is unclear what your code wants to do. It writes only if the file gets created, however in this case the file is also closed. Otherwise you do not write anything, but the file remains open...

  12. #12
    Nokia Developer Champion
    Join Date
    Jun 2008
    Location
    Gurgaon,India
    Posts
    4,112

    Re: application get closed when opening file and file doesnt exist

    you do not need to search anything, but better would be , when you code then keep in mind of all the deletion and other related things properly , so as to minimize the memory leakage.

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

    Re: application get closed when opening file and file doesnt exist

    I guess you are getting a memory leak confused with a leave resulting in an out of memory situation. Two entirely different things, and both of which unrelated to deleting a file.
    Better get a few of the basic concepts of Symbian understood before proceeding too far.

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

    Re: application get closed when opening file and file doesnt exist

    HI amit,
    1) On the line, where you are trying to open the file, you hav written - [User::LeaveIfError], it means if the file does not exists,
    the code will leave at that point with KErrNotFound.
    Leave occurs when the resource, that you want to access does not exists.
    Instead of that, you check whether file exists or not.



    2)
    if(!Exists)
    {
    DeleteFile.Create(DeleteSession,(_L("C:\\Data.txt")),EFileShareAny|EFileRead|EFileWrite);
    }

    Regards,
    himanshu.

Similar Threads

  1. N93, opening SWF file, FILE ERROR message
    By NokiaMobileUser in forum General Development Questions
    Replies: 3
    Last Post: 2010-05-06, 16:20
  2. Write to FileSystem when fileconn.dir.videos doesnt exist?
    By Erik.Ingman in forum Mobile Java General
    Replies: 0
    Last Post: 2008-03-31, 11:56
  3. know file size after opening the file.
    By Tanya in forum Symbian User Interface
    Replies: 2
    Last Post: 2007-03-20, 13:31
  4. know file size without opening the file
    By venunet in forum Symbian
    Replies: 7
    Last Post: 2007-03-20, 08:58

Posting Permissions

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