×

Discussion Board

Results 1 to 3 of 3
  1. #1
    Registered User
    Join Date
    Jul 2013
    Posts
    73

    Problem with deleting CActive object after code completion.

    Hello, i encountered a problem with stopping (deleting) CActive .

    I wanted to save images async* but don't know how to delete the object after save is done.


    This is a code but i shouldn't show you it because it has nothing to do with it:
    Code:
    /*
     ============================================================================
     Name		: JpgSaver.cpp
     Author	  : 
     Version	 : 1.0
     Copyright   : Your copyright notice
     Description : CJpgSaver implementation
     ============================================================================
     */
    
    #include "JpgSaver.h"
    #include "TMobEyeTh.h"
    
    
    
    
     
    
    CJpgSaver::CJpgSaver(CFbsBitmap& aBitmap,TDesC& aFileName)
    :CActive(EPriorityStandard),iBitmap(aBitmap), iFileName(aFileName)   
    	{
    
    	}
    
    CJpgSaver* CJpgSaver::NewLC(CFbsBitmap& aBitmap,TDesC& aFileName)
    	{
    	CJpgSaver* self = new (ELeave) CJpgSaver(aBitmap, aFileName);
    	CleanupStack::PushL(self);
    	self->ConstructL();
    	return self;
    	}
    
    CJpgSaver* CJpgSaver::NewL(CFbsBitmap& aBitmap, TDesC& aFileName)
    	{
    	CJpgSaver* self = CJpgSaver::NewLC(aBitmap, aFileName);
    	CleanupStack::Pop(); // self;
    	return self;
    	}
    
    void CJpgSaver::ConstructL()
    	{
    	User::LeaveIfError(iTimer.CreateLocal()); // Initialize timer
    	CActiveScheduler::Add(this); // Add to scheduler
    	saving = false;
    	saved = false;
    	}
    
    CJpgSaver::~CJpgSaver()
    	{
    	Cancel(); // Cancel any request, if outstanding
    	iTimer.Close(); // Destroy the RTimer object
    	delete iEncoder;  
    	}
    
    void CJpgSaver::DoCancel()
    	{
    	Cancel();
    	iTimer.Cancel();
    	}
    
    void CJpgSaver::StartL(TTimeIntervalMicroSeconds32 aDelay)
    	{
    	Cancel(); // Cancel any request, just to be sure
    	saving = false;
    	iTimer.After(iStatus, aDelay); // Set for later
    	SetActive(); // Tell scheduler a request is active
    	}
    
    void CJpgSaver::RunL()
    	{
    	
    	
    //	if (saved == true) {Deque(); DoCancel();  delete this; return;}
    // if( saving == false) {
        iEncoder = CImageEncoder::FileNewL(CCoeEnv::Static()->FsSession(),iFileName, KMimeType);
        iEncoder->Convert( &iStatus, iBitmap);
        FrameFlush = false;
    //saved = true;
    //}  
    //    saving = true;
    
        iTimer.After(iStatus, 10000); // Set for later
        	SetActive(); 
    	}
    
    TInt CJpgSaver::RunError(TInt aError)
    	{
    	return aError;
    	}
    Code:
    /*
     ============================================================================
     Name		: JpgSaver.h
     Author	  : 
     Version	 : 1.0
     Copyright   : Your copyright notice
     Description : CJpgSaver declaration
     ============================================================================
     */
    
    #ifndef JPGSAVER_H
    #define JPGSAVER_H
    
    #include <e32base.h>	// For CActive, link against: euser.lib
    #include <e32std.h>		// For RTimer, link against: euser.lib
    #include <f32file.h>
    #include <gdi.h>
    #include <ImageConversion.h>
    #include <coemain.h>
    #include "stringunit.h"
    
    _LIT8(KMimeType,"image/jpeg");
    
    class CJpgSaver : public CActive
    	{
    public:
    	bool saving;
    	bool saved;	
    	 CJpgSaver(CFbsBitmap& aBitmap,TDesC& aFileName);
    	~CJpgSaver();
    
    	static CJpgSaver* NewL(CFbsBitmap& aBitmap, TDesC& aFileName);
    	static CJpgSaver* NewLC(CFbsBitmap& aBitmap, TDesC& aFileName);
    	
    	void StartL(TTimeIntervalMicroSeconds32 aDelay);
    
    private:
    
        
        void ConstructL();
    	void RunL();
    	void DoCancel();
        TInt RunError(TInt aError);
    	
    	TInt iState; // State of the active object
    	RTimer iTimer; // Provides async timing service
        CFbsBitmap& 		iBitmap;
        CImageEncoder* 		iEncoder;
        TDesC& iFileName;
    	};
    
    #endif // JPGSAVER_H

    CActive execution:
    Code:
    void SaveAsJPG(CFbsBitmap * bmp)
    	{
    SNAPSHOTAKEN = true;
    FrameFlush = true;
    globalcount = globalcount + 1;
    
    			 		AnsiString fname;
    			 		fname.IIFI("E:\\Temp\\Image");
    			 		fname = fname + IntToStr(globalcount);
    			 	//	fname = fname ;
    			 		fname = fname + ".jpg";
    
    	
    	ASYNCjpegSavingService= CJpgSaver::NewL(*bmp, *fname.AnsiStringToHBufLC());
    	ASYNCjpegSavingService->StartL(100);
    	
    
    	
    	}
    So as you can see i create a CActive and then (in RunL function i save bitmap to jpeg file:
    ASYNCjpegSavingService= CJpgSaver::NewL(*bmp, *fname.AnsiStringToHBufLC());
    ASYNCjpegSavingService->StartL(100);
    I tried many ways but i think i should implement a callback function (that will inform me about file save), anyway i do not have an idea how.
    Cheers.

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

    Re: Problem with deleting CActive object after code completion.

    You are right, you should implement the callback. It would be just a interface definition which you would implement in the calling class, and then give the active object a reference to the object, so it could call the function of the interface when needed, Wiki is full of examples for that with active obejcts so you could maybe do a bit of searching.

    Then try out the implementation, and once you have problems, then explain them in details, so we could have better chance on helping.

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    45

    Re: Problem with deleting CActive object after code completion.

    Hi malepeta.
    There are many issues in your code, so remove them and, probably everything will work fine without magical suggestions.

    - corrupted cleanup stack with memory leak is here:
    ASYNCjpegSavingService= CJpgSaver::NewL(*bmp, *fname.AnsiStringToHBufLC());
    The HBufC object was allocated and will be lost without CleanupStack::PopAndDestroy() below.


    - active object itself should contain only ONE async operation. In your sources you use 2 ones: Timer & CImageEncoder. This code will never work fine and requires separate active objects. It's clear from RunL() where 2 asynchronous operations are launched in the same time.
    - in DoCancel() you must stop async work. If the class is related to image conversion, so you HAVE TO cancel convertion request with iEncoder->Cancel().
    - recursive call of DoCancel() will be here:
    Code:
    void CJpgSaver::DoCancel()
    	{
    	Cancel();		// <=== calls DoCancel() respectively, so go out of stack
    	iTimer.Cancel();
    	}
    - void SaveAsJPG(CFbsBitmap * bmp) should have name, at least with L ending: SaveAsJpgL() because of being with leave code inside...

    To avoid further crucial errors in your 'applications' you should:
    1) read a book about active objects. Without understanding of the primary thing it's not possible to write Symbian application.
    2) carefully read API description and find a time to look out through examples and forum topics.
    3) read a book about usual workflows with cleanup stack (CleanupStack).

Similar Threads

  1. Problem in Deleting Resource Object in different thread!
    By manish.agarw in forum Symbian C++
    Replies: 0
    Last Post: 2009-11-23, 12:48
  2. RunL(); CActive object problem.
    By Dvirus in forum Symbian C++
    Replies: 12
    Last Post: 2008-07-31, 17:44
  3. CActive object problem
    By xchewy in forum Symbian C++
    Replies: 39
    Last Post: 2008-05-10, 20:40
  4. A stranger problem in CActive Object
    By jerrysdfha in forum Symbian User Interface
    Replies: 1
    Last Post: 2008-04-14, 12:58
  5. problem in CActive Object... in make a dial...
    By marycore in forum Symbian C++
    Replies: 1
    Last Post: 2006-09-12, 09:50

Posting Permissions

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