What's wrong with my code? Thread+CleanupStack+CEZGZipToFile
| Mon, 2006-11-06 09:32 | |
|
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. |
|






Forum posts: 732
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?
Forum posts: 33
<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.
  {
  __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
Forum posts: 27
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.
Forum posts: 732
Forum posts: 27
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.
Forum posts: 27
Taking you forward.
Forum posts: 2
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();
}