×

Discussion Board

Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    Regular Contributor
    Join Date
    Jan 2008
    Posts
    61

    Access to files crashes the program

    Hi, all. I need help.

    The code below crashes on emulator inside the ifstream destructor:

    #include <fstream>

    using namespace std;

    TAny CUniDictListView::ConstructL()
    {
    {
    ifstream file("c:\\file");

    } // the crash is here (kern exec 3)
    }

    Use of C standard library (the same libstdcpp.lib) doesn't help:

    #include <stdio.h>

    TAny CUniDictListView::ConstructL()
    {
    FILE *f = fopen("c:\\file","r"); // the crash is here (kern exec 3)

    fclose(f);
    }

    Can you give me some reason?

  2. #2
    Registered User
    Join Date
    Jun 2007
    Location
    Mumbai, India
    Posts
    1,998

    Re: Access to files crashes the program

    Hi,

    Check whether the file you are trying to open is present at the mentioned path.

  3. #3
    Regular Contributor
    Join Date
    Jan 2008
    Posts
    61

    Re: Access to files crashes the program

    The mentioned filename is only a string (nonexistent file), but what's the difference? In the first case ifstream object must remain closed, in the second one - fopen returns NULL;

  4. #4
    Registered User
    Join Date
    Dec 2006
    Posts
    2,280

    Re: Access to files crashes the program

    Hi,

    If the file doesn't exist then these are programming errors, you should always check if the file exists or specify that you want it created if it doesn't exist already. This is regardless of the environment.

    Ideally, Symbian OS with Open C/C++ would behave in a totally POSIX compliant manner for all programming errors but it doesn't always.

    However, if you write correct code which performs the necessary checks then it should work fine.

    Sorcery

    P.S. There is another mistake in your code - you shouldn't use TAny for void, only TAny* for void*. The idea is it means pointer to anything, rather than pointer to nothing, which void* seems to imply. I know TAny is just a typedef, but that's the logic behind it. Personally I think this was just an engineer creating an unnecessary break from standards because they personally didn't like something.

  5. #5
    Regular Contributor
    Join Date
    Jan 2008
    Posts
    61

    Re: Access to files crashes the program

    Hi, Sorcery-ltd. Thank you for reply.

    If the file doesn't exist then these are programming errors, you should always check if the file exists or specify that you want it created if it doesn't exist already. This is regardless of the environment.
    Here is the text from MSDN documentation (no Microsoft specific):

    Code:
    FILE *fopen( 
       const char *filename,
       const char *mode 
    );
    FILE *_wfopen( 
       const wchar_t *filename,
       const wchar_t *mode 
    );
     
    
    Parameters
    filename
    Filename.
    
    mode
    Type of access permitted.
    
    Return Value
    Each of these functions returns a pointer to the open file. A null pointer value indicates an error. ...
    and the classic fstream example from stl book:

    Code:
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    void func(const string &filename)
    {
      ifstream file(filename.c_str());
    
      if (file)
      {
        ... // is opened
      }
      else 
      {
        ... // error
      }
    }
    By the way, I've tried a name of an existent nonempty file and have got the same result...

  6. #6
    Registered User
    Join Date
    Dec 2006
    Posts
    2,280

    Re: Access to files crashes the program

    Sorry, I should have read your post more carefully. Of course you should be able to call fopen() with an invalid file and get a NULL pointer back, that's the most common way to check if the file exists. I thought it was effectively fclose(NULL) that was causing your error (although that SHOULD also work, it is a programming error).

    As for ifstream, you're right, that code should work too, although it would be better to check the result and do something about it rather than just let the object go out of scope (I assume you do in your real code rather than this minimal example though).

    Are you linking against libc.lib and NOT linking against estlib.lib. This is what you should be doing. Anything else is likely to be trying to use the wrong library on the emulator (and will almost definitely crash, if it links at all on the target device).

    If that's the case you can get all sorts of panics - including Kern-Exec 3.

    If your libraries are correct, make sure you're reading a file from a public directory and not one that is protected by PlatSec - although in that case you should get KErrPermissionDenied, not a Kern-Exec 3 panic.

    Sorcery

  7. #7
    Regular Contributor
    Join Date
    Jan 2008
    Posts
    61

    Re: Access to files crashes the program

    As I said I linked against the libstdcpp.lib. Is it ok?

  8. #8
    Regular Contributor
    Join Date
    Jan 2008
    Posts
    61

    Re: Access to files crashes the program

    My mistake was in linking against libstdcpp.lib without libc.lib. Thank you all.
    Last edited by ababo; 2008-10-07 at 17:22.

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    17

    Re: Access to files crashes the program

    I'm having a similar problem with OpenC or OpenCPP while trying to open files.
    My code was working fine on early stages on the emulator and the test device(N73), but when the code grew a little it keeps working on the emulator without charges on the mpp or the files and on the device it crashes without errors, the program simply closes when trying open a simple text file. I've changed the code from C FILE stream to C++ ifstream but it keeps crashing.
    I´ve tried to remake a early backup of the code to work the same way the new code and now it reads the file and crashes when opening the next file.
    Can someone points me a direction to correct this 'cause I'm without ideas .

    Sorry about the bad english and I'm new to programing.

  10. #10
    Registered User
    Join Date
    Dec 2006
    Posts
    2,280

    Re: Access to files crashes the program

    Hi,

    Try enabling the extended panic codes on the device:
    http://wiki.forum.nokia.com/index.ph...ded_panic_code
    Hopefully you'll see what the error is.

    It might be a Platform Security error (KErrPermissionDenied usually), depending on the locations of the files you're trying to open.

    Hard to tell without some more info though.

    Sorcery

  11. #11
    Registered User
    Join Date
    Apr 2008
    Posts
    17

    Re: Access to files crashes the program

    Quote Originally Posted by Sorcery-ltd View Post
    Hi,

    Try enabling the extended panic codes on the device:
    http://wiki.forum.nokia.com/index.ph...ded_panic_code
    Hopefully you'll see what the error is.

    It might be a Platform Security error (KErrPermissionDenied usually), depending on the locations of the files you're trying to open.

    Hard to tell without some more info though.

    Sorcery

    I'm loading files from the memory card and moved then to the aplication folder on private but the error continued. I´ll see the panic codes.

  12. #12
    Registered User
    Join Date
    Apr 2008
    Posts
    17

    Re: Access to files crashes the program

    Using the errrd file now it shows a error code:
    ---------------------
    App. closed
    Local-641cb250
    KERN-EXEC 3
    -----------------------

    Any ideas?

  13. #13
    Registered User
    Join Date
    Dec 2006
    Posts
    2,280

    Re: Access to files crashes the program

    Have you checked that you don't have a similar problem to the original one posted in this thread?

    Which libraries are you linking against? (Probably easiest if you cut and paste your MMP file contents to this thread).

    Sorcery

  14. #14
    Registered User
    Join Date
    Apr 2008
    Posts
    17

    Re: Access to files crashes the program

    It's like the original because I was about to write the same thread name when I read this .
    The difference is that I've not been able to see the error code before( that is the same - or no?), I'm linking to the right(I think) libraries and the code stopped to work from one build to another(it worked before).
    When I get home I'll copy the mmp to post here.
    Thanks to the answers.

    PS. It still works on the emulator.
    Last edited by AndreBotelho; 2008-11-14 at 15:37. Reason: P.S.

  15. #15
    Registered User
    Join Date
    Apr 2008
    Posts
    17

    Re: Access to files crashes the program

    The mmp of the my project is using these include and libraries:

    ...

    USERINCLUDE ..
    SYSTEMINCLUDE \epoc32\include
    SYSTEMINCLUDE \epoc32\include\stdapis
    SYSTEMINCLUDE \epoc32\include\stdapis\sys
    SYSTEMINCLUDE \epoc32\include\stdapis\stlport
    SYSTEMINCLUDE \epoc32\include\stdapis\stlport\stl
    SYSTEMINCLUDE \epoc32\include\SDL


    LIBRARY libstdcpp.lib
    LIBRARY libc.lib
    LIBRARY libpthread.lib
    LIBRARY euser.lib
    LIBRARY avkon.lib
    LIBRARY apparc.lib
    LIBRARY cone.lib
    LIBRARY eikcore.lib
    LIBRARY sdl.lib
    LIBRARY SDL_image.lib
    LIBRARY SDL_ttf.lib
    //LIBRARY sdlexe.lib

    //STATICLIBRARY sdlmain.lib


    #ifdef ENABLE_ABIV2_MODE
    DEBUGGABLE
    #endif

    ...

    EPOCHEAPSIZE 0x200000 0x2000000

    ...

    CAPABILITY ReadUserData WriteUserData

    OPTION CW -wchar_t on
    MACRO _WCHAR_T_DECLARED
    I'm using 3rd ed sdk and carbide c++ as IDE.

Similar Threads

  1. Program crashes when calling SetItemTextArray
    By Hans456 in forum Symbian
    Replies: 3
    Last Post: 2007-07-02, 16:58
  2. Access to Media Files
    By h3gg3m in forum Mobile Java General
    Replies: 5
    Last Post: 2006-06-29, 09:26
  3. how to Select Access Point by program?
    By haha@java in forum Symbian Networking & Messaging (Closed)
    Replies: 2
    Last Post: 2004-04-01, 10:57
  4. how to access directories and files from J2ME app
    By johnmobile in forum Mobile Java General
    Replies: 6
    Last Post: 2004-01-23, 21:25

Posting Permissions

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