What's wrong with my code? Thread+CleanupStack+CEZGZipToFile

Login to reply to this topic.
Mon, 2006-11-06 09:32
Joined: 2006-07-26
Forum posts: 27
The code is below:

// It's a static thread function
TInt CSingleConsumer::ThreadFun( TAny* aPara )
{
__UHEAP_MARK;
CTrapCleanup* cleanup = CTrapCleanup::New();
if( cleanup == NULL )
{
return KErrNoMemory;
}

TInt err = KErrNone;
TRAP( err, ThreadFunCleanup( aPara ) ); // invoke the function which use CleanupStack

if( err )
return KErrNoMemory;

delete cleanup;
__UHEAP_MARKEND;

return KErrNone;

}

TInt CSingleConsumer::ThreadFunCleanup( TAny* aPara )
{
ThreadPara* iPara = ( ThreadPara* )aPara;

RFs fs;
fs.Connect();

RFile file;
file.Replace( fs, _L("C:\\test.txt"), EFileRead|EFileWrite|EFileShareAny );

// I'm sure that C:\test.txt.gz exists and
// the code below runs at no thread project
// But here the line below will crash!!!
CEZGZipToFile* zip = CEZGZipToFile::NewL( fs, _L("C:\\test.txt.gz"), file );


delete zip;

file.Close();
fs.Close();
}
CEZGZipToFile* zip = CEZGZipToFile::NewL( fs, _L("C:\\test.txt.gz"), file ); will crash, I don't know why.

I've already creata a cleanup stack for the thread and also use TRAP, what's wrong with my code? Please help me!



Thank you!
Jimmy

Taking you forward.


Mon, 2006-11-06 10:05
Forum Nokia Champion
Joined: 2004-05-26
Forum posts: 732
Re: What's wrong with my code? Thread+CleanupStack+CEZGZipToFile
Check what the SDK Help says for E32USER-CBase 71:


Quote
This panic is raised when TRAPs have been nested and an attempt is made to exit from a TRAP nest level before all the cleanup items belonging to that level have been popped off the cleanup stack.

There must be the same number of items on the cleanup stack on entering a TRAP harness as there is on exiting. In other words, anything that is pushed onto the cleanup stack inside a TRAP harness must be popped off before leaving the harness.

For example, the following code avoids this panic when FooLC() does not leave, by explicitly popping pointer before the end of the harness:

TRAPD(error, pointer = FooLC(); CleanupStack::Pop(pointer));
See also How to use TRAP.


These are the things I've noticed:

1. Each time you return from ThreadFun function you haven't deleted the cleanup.

2. You've pushed the fs into cleanupstack but haven't popped it.

3. You’re neither pushing nor popping zip in to cleanupstack.

4. What does the iPara do in your code?

Mon, 2006-11-06 23:14
Joined: 2004-11-21
Forum posts: 33
Re: What's wrong with my code? Thread+CleanupStack+CEZGZipToFile
Quote from: rensijie
The code is below:

<snip>

CEZGZipToFile* zip = CEZGZipToFile::NewL( fs, _L("C:\\test.txt.gz"), file ); will crash, I don't know why.

I've already creata a cleanup stack for the thread and also use TRAP, what's wrong with my code? Please help me!



Thank you!
Jimmy


There are some leave safety issues with the code that should come through with the version below.  See if it works better.

Code:
TInt CSingleConsumer::ThreadFun( TAny* aPara )
    {
    __UHEAP_MARK;
    CTrapCleanup* cleanup = CTrapCleanup::New();
    if( cleanup == NULL )
        {
        __UHEAP_MARKEND; // premature bail-out, need to take care of the marker.
        return KErrNoMemory;
        }

    TRAPD( err, ThreadFunCleanupL( aPara ) ); // invoke the function which use CleanupStack

    delete cleanup;
    __UHEAP_MARKEND;

    return err; // Is always KErrNone of no leave occurs.
    }

void CSingleConsumer::ThreadFunCleanupL( TAny* aPara )
    {
    ThreadPara* para = static_cast< ThreadPara* >( aPara ); // don't want to use C-style casts.

    RFs fs;
    User::LeaveIfError( fs.Connect() );
    CleanupClosePushL( fs ); // Need to close in a leave situation

    RFile file;
    User::LeaveIfError( file.Replace( fs, _L("C:\\test.txt"), EFileRead|EFileWrite|EFileShareAny );
    CleanupClosePushL( file ); // Need to close in a leave situation

    // I'm sure that C:\test.txt.gz exists and
    // the code below runs at no thread project
    // But here the line below will crash!!!
    CEZGZipToFile* zip = CEZGZipToFile::NewLC( fs, _L("C:\\test.txt.gz"), file ); // Use NewLC instead :)

    // Do your thing here

    CleanupStack::PopAndDestroy( &fs ); // zip, file, fs
    }

Regards, Bo

Tue, 2006-11-07 08:53
Joined: 2006-07-26
Forum posts: 27
Re: What's wrong with my code? Thread+CleanupStack+CEZGZipToFile
Thank you vin2ktalks and Bo, but it's not working using both of your method.

The most strange thing is if I don't instantiate CEZGZipToFile, everything is fine. If I do it, TRAPD will catch the error, BUT THE ERROR CODE IS 0! MEANS KERRNONE!

I don't know why.

Please help...

Taking you forward.

Tue, 2006-11-07 09:20
Forum Nokia Champion
Joined: 2004-05-26
Forum posts: 732
Re: What's wrong with my code? Thread+CleanupStack+CEZGZipToFile
Wed, 2006-11-08 02:21
Joined: 2006-07-26
Forum posts: 27
Re: What's wrong with my code? Thread+CleanupStack+CEZGZipToFile
I did a test, I use:

User::Panic( _L(""), err );

to check the error code when

CEZGZipToFile::NewL( fs, _L("C:\\test.txt.gz"), file );

leaves, and the error code is -4, const TInt KErrNoMemory=(-4);

why??? Did I miss something?

Taking you forward.

Wed, 2006-11-08 03:59
Joined: 2006-07-26
Forum posts: 27
Re: What's wrong with my code? Thread+CleanupStack+CEZGZipToFile
IÂ’ve solved the problem, the cause is the custom-built Cleanup Stack itself will allocate much memory on the threadÂ’s heap, so when allocate another object on the heap, the program crashed(no memory). I increased the initial heap memory size of the thread the problem solved.


Taking you forward.

Tue, 2007-09-25 11:50
Joined: 2007-09-24
Forum posts: 2
Help Please

I have this function in one of my project , got from a web site. But not working. Please Help!!

void DeCompressAFile(TDesC& aSrcName, TDesC& aDstName)
{
TBuf8<256> msg8;TBuf<256> msg16;

RFs fs;User::LeaveIfError(fs.Connect());
fs.SetSessionPath(_L("C:\\"));
RFile file;
TInt err = file.Open(fs,aDstName,EFileRead);

if(err != KErrNone)
{
msg8.Format(_L8("Error Reading File"));
msg16.Copy(msg8);
ERRDLG(msg16);
return;
}
msg8.Format(_L8("Before"));
msg16.Copy(msg8);
ERRDLG(msg16);

CEZGZipToFile *zip = CEZGZipToFile::NewLC(fs, aSrcName, file); //The function stops here witout crash and returns.

msg8.Format(_L8("CEZGZipToFile::before loop"));
msg16.Copy(msg8);
ERRDLG(msg16);

TInt tempi = 0;
while(zip->InflateL())
{
tempi++;
}

msg8.Format(_L8("Completed"));
msg16.Copy(msg8);
ERRDLG(msg16);

CleanupStack::PopAndDestroy();
file.Close();
fs.Close();
}

  • Login to reply to this topic.