error in opening a file!!!!!!!!!!!!!!!!!!

Login to reply to this topic.
Tue, 2007-11-20 08:17
Joined: 2007-11-02
Forum posts: 37

Hi,
I am using S60 3rd edn FP1
and N95 phone

The following code crashes at run time while opening file

in .h file
---------------
RFile* iFile;

in .cpp file
-----------------
#define KDirPictures PathInfo::ImagesPath()
#define KPhoneRootPath PathInfo::PhoneMemoryRootPath()
_LIT (KFileName2,"Snaps.jpg");

TFileName fileName(KPhoneRootPath);
fileName.Append(KDirPictures);
fileName.Append(KFileName2);//after this value of fileName is C:/data/images/Snaps.jpg

RFs afs;
User::LeaveIfError(afs.Connect());
CleanupClosePushL(afs);
TInt err=iFile->Open(afs,fileName,EFileShareExclusive|EFileWrite);//The application crashes after this line
CleanupClosePushL(*iFile);

ALSO TRIED WITH
TInt err=iFile->Open(CCoeEnv->Static()->FSSession,fileName,EFileShareExclusive|EFileWrite);
It too crashes


Tue, 2007-11-20 09:05
Joined: 2007-05-15
Forum posts: 14
Re: error in opening a file!!!!!!!!!!!!!!!!!!

Why are you pushing iFile onto the CleanupStack??

Tue, 2007-11-20 17:58
Joined: 2007-09-23
Forum posts: 159
Re: error in opening a file!!!!!!!!!!!!!!!!!!

What does R mean in Symbian's naming scheme?

What happens when you don't understand C or C++ and try and use object pointers that don't point to anything?

Wed, 2007-11-21 07:20
Joined: 2007-11-02
Forum posts: 37
Re: error in opening a file!!!!!!!!!!!!!!!!!!

I think the program crashes before reaching the CleanupClosePushL();

Can you brief "object pointers that don't point to anything".

May be I am wrong..
But I dont understand how.

Can you be breif.

I think RFile-> OPen() should have initialised.

Wed, 2007-11-21 08:35
Joined: 2007-09-20
Forum posts: 114
Re: error in opening a file!!!!!!!!!!!!!!!!!!

Hi,

Instead of using a RFile pointer "*iFile", try using RFile object "iFile".
Also there is no need to push the R class object on the clean up stack.


Chao,
Raghav

Wed, 2007-11-21 09:03
Joined: 2005-11-20
Forum posts: 1241
Re: error in opening a file!!!!!!!!!!!!!!!!!!

Also there is no need to push the R class object on the clean up stack.

I don't think that's true. Depending on how the code continues after the file open (we do not see that) there would be no other way than using the cleanup stack to make sure that the file will be closed if a Leave occurs.

After all, that's why CleanupClosePushL exists.

Or did I misunderstand your post?


René Brunner

Wed, 2007-11-21 09:17
Joined: 2007-09-20
Forum posts: 114
Re: error in opening a file!!!!!!!!!!!!!!!!!!

Hi Rene,

I don't think that's true. Depending on how the code continues after the file open (we do not see that) there would be no other way than using the cleanup stack to make sure that the file will be closed if a Leave occurs.

You are right. It was my mistake. Thanks for correcting.


Chao,
Raghav

Thu, 2007-11-22 09:27
Joined: 2007-05-15
Forum posts: 14
Re: error in opening a file!!!!!!!!!!!!!!!!!!

Let's clarify something, R class objects should be pushed onto the cleanup stack but in this case RFile is an instance variable so it shouldn't be placed on cleanup stack.

Fri, 2007-11-23 19:36
Joined: 2007-09-23
Forum posts: 159
Re: error in opening a file!!!!!!!!!!!!!!!!!!

Lets clarify evern further, there is only a need to push R class objects (or C objects) onto the cleanup stack provided they aren't member variables and provided the code which follows their opening may leave - many people do this sorth of thing (not just with R objects)

RSomething x;
x.Open();
CleanupClosePushL(x);
some code with can't leave
CleanupStack::PopAndDestory(x);

Sat, 2007-11-24 01:43
Joined: 2007-11-02
Forum posts: 37
Re: error in opening a file!!!!!!!!!!!!!!!!!!

ok!
I still have some confusions...
What is the difference between these 3 approaches.I think all of them are the same .Are there any differences??????

APPROACH 1:
-----------------------
RFile iFile1;
iFile1.Open(afs,fileName,EFileShareExclusive|EFileWrite);

APPROACH 2:
-----------------------
RFile* iFile2;
(*iFile2).Open(afs,fileName,EFileShareExclusive|EFileWrite);

APPROACH 3:
-----------------------
RFile* iFile3;
iFile3->Open(afs,fileName,EFileShareExclusive|EFileWrite);

AND ONE MORE THING THAT I NEED TO CLEAR UP IS:_
-I dont need to push the iFile object because it is a member of the class.
-But still pushing them into the cleanupstack should in my opinion be an additional security step.Should that be an error?
-Moreover all the pointer variables which are members of a class are pushed into the cleanupStack. R classes are also handles to other objects.handles are somewhat similar to pointers.Then why not push it?

Regards!

Sat, 2007-11-24 03:05
Joined: 2007-11-02
Forum posts: 37
Re: error in opening a file!!!!!!!!!!!!!!!!!!

I started another helloworld program and copied the lines of code into the ConstructL() of appui.

in appui.h file
----------------
RFile* iFile;

in appui.cpp file
------------------
#define KDirPictures PathInfo::ImagesPath()
#define KPhoneRootPath PathInfo::PhoneMemoryRootPath()
_LIT (KFileName2,"Snaps.jpg");

TFileName fileName(KPhoneRootPath);
fileName.Append(KDirPictures);
fileName.Append(KFileName2);

RFs afs;
User::LeaveIfError(afs.Connect());
CleanupClosePushL(afs);

_LIT(KCameraStart,"asdasd");
CConsoleBase* gConsole;
gConsole=Console::NewL(KCameraStart,TSize(40,40));
CleanupStack::PushL(gConsole);
gConsole->ClearScreen();
gConsole->Printf(KCameraStart);
gConsole->Getch();
CleanupStack::PopAndDestroy(gConsole);
TInt err2=iFile->Open(afs,fileName,EFileShareExclusive|EFileWrite);

I ran the application in the emulator and the application:-
1.printed to the console
2.then crashed with "KERN-EXEC3"

Then I made following changes:-

in appui.h file
----------------
made
RFile* iFile to
RFile iFile

in .cpp file
-------------
made
TInt err2=iFile->Open(afs,fileName,EFileShareExclusive|EFileWrite);
as
TInt err2=iFile.Open(afs,fileName,EFileShareExclusive|EFileWrite);

I ran the application in the emulator and the application:-
1.printed to the console
2.then crashed with "E32-USER Cbase71"

Sat, 2007-11-24 17:09
Joined: 2007-11-02
Forum posts: 37
Re: error in opening a file!!!!!!!!!!!!!!!!!!

Sorry,
The code works for
Approach 1
but
not for approach 2 and 3

May I know what is the difference between 3 approaches, I thought them to be simmilar

Thanks a lot.

Mon, 2007-11-26 09:57
Joined: 2007-09-20
Forum posts: 114
Re: error in opening a file!!!!!!!!!!!!!!!!!!

Hi,

Sorry,
The code works for
Approach 1
but
not for approach 2 and 3

If you are using pointers the memory pointed by them should be valid. In your case you have to instantiate the RFile type


Chao,
Raghav

Tue, 2007-11-27 18:47
Joined: 2007-09-23
Forum posts: 159
Re: error in opening a file!!!!!!!!!!!!!!!!!!

Trying to learn SYmbian before learning C++ is going to be difficult for you.

In approach 2 and 3 you have not created an object, you have only created a pointer to an object.

Thu, 2007-12-06 06:28
Joined: 2007-11-02
Forum posts: 37
Re: error in opening a file!!!!!!!!!!!!!!!!!!

Thanks,

It is not that difficult till the forum is there and the responses I get.
What else can be better than learning from all the peoples on this and other forums...
"peoples" include you too.

I am thankfull..............

Whatever may be the result, I wont leave trying

Regards!

  • Login to reply to this topic.