×

Discussion Board

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

    Inherited CActive class creation problem

    Hello i want to make a thread andl i found: http://developer.nokia.com/Community...Active_Objects

    It seems better than 'normal' thread.
    So i started to change the code provided in the sample. ( system file monitor)

    i ended with this:

    Code:
    #include <e32std.h>
    #include <e32cons.h>
    #include <e32base.h>
    #include "stringunit.h"
    
    class TMobEyeThread : public CActive
        {
     
    public:
     
        ~TMobEyeThread();
        static TMobEyeThread* NewL();
        void StartThread();
        void StopThread();    
     AnsiString k;
    
     
        TMobEyeThread();
        void ConstructL();
     
    protected:
     
        virtual void RunL();            // Inherited from CActive.
        virtual void DoCancel();  
      
        };
     
    TMobEyeThread::TMobEyeThread() : CActive(EPriorityStandard)
        { 
    	CActiveScheduler::Add(this); 
    	k = "";
        }
     
    void TMobEyeThread::ConstructL()
        {                             
     
    
        }
     
    TMobEyeThread* TMobEyeThread::NewL()
        {
    
        } 
    
    TMobEyeThread::~TMobEyeThread()
        {// Stop any outstanding requests.
        Cancel();
        }
     
    void TMobEyeThread::StartThread()
        { 
        SetActive();              
        }
    
    void TMobEyeThread::StopThread()
    	{
    	Cancel();
    	}
     
    // Event handler method.
     
    void TMobEyeThread::RunL()
        {
    
    	k = k + "*";
     
        SetActive();
     
     
        }


    My first issue is that i can't create class with
    Code:
    	PC_EYE = new TMobEyeThread();

    And another (primary i think)

    compiler stops at

    Code:
    TMobEyeThread* TMobEyeThread::NewL()
        {
    
        }
    and says: 'return value expected'




    other compiler errors are (but i think they will disappear when i make a proper constructor for this calss)
    mwldsym2.exe: Multiply defined section: TMobEyeThread::TMobEyeThread(void) (??0TMobEyeThread@@QAE@XZ) in
    mwldsym2.exe: files MobEyeContainerView.o, MobEyeContainer.o
    mwldsym2.exe: Multiply defined section: void TMobEyeThread::ConstructL(void) (?ConstructL@TMobEyeThread@@QAEXXZ) in
    mwldsym2.exe: files MobEyeContainerView.o, MobEyeContainer.o
    mwldsym2.exe: Multiply defined section: class TMobEyeThread * TMobEyeThread::NewL(void) (?NewL@TMobEyeThread@@SAPAV1@XZ) in
    mwldsym2.exe: files MobEyeContainerView.o, MobEyeContainer.o
    mwldsym2.exe: Multiply defined section: TMobEyeThread::~TMobEyeThread(void) (??1TMobEyeThread@@UAE@XZ) in
    mwldsym2.exe: files MobEyeContainerView.o, MobEyeContainer.o
    mwldsym2.exe: Multiply defined section: void TMobEyeThread::StartThread(void) (?StartThread@TMobEyeThread@@QAEXXZ) in
    mwldsym2.exe: files MobEyeContainerView.o, MobEyeContainer.o
    mwldsym2.exe: Multiply defined section: void TMobEyeThread::StopThread(void) (?StopThread@TMobEyeThread@@QAEXXZ) in
    mwldsym2.exe: files MobEyeContainerView.o, MobEyeContainer.o
    mwldsym2.exe: Multiply defined section: void TMobEyeThread::RunL(void) (?RunL@TMobEyeThread@@MAEXXZ) in
    mwldsym2.exe: files MobEyeContainerView.o, MobEyeContainer.o
    mwldsym2.exe: Undefined symbol: 'void TMobEyeThread:oCancel(void) (?DoCancel@TMobEyeThread@@MAEXXZ)'
    mwldsym2.exe: referenced from 'const TMobEyeThread::`vftable' (??_7TMobEyeThread@@6B@~)' in MobEyeContainer.o

    So how do i create such classes?

  2. #2
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,699

    Re: Inherited CActive class creation problem

    You should really read that essential idioms and related things in the SDK.
    NewL (and NewLC) is supposed to be a two-phased constructor. The entire story is about resource protection, the Cleanup Stack. The normal new operator of C++ allocates the memory and invokes the constructor. If there is no memory, it fails. If there is memory, the constructor starts. However it may happen that the constructor also wants to allocate resources and/or memory. The real problem is when some of these resources get successfully allocated, but at the end something fails. In that case the entire thing result in an error, however in the Symbian implementation the resources allocated on the way do not get freed. And since you got an error at the new operator there is no pointer available for issuing a delete. The allocated resources are leaked, and they remain leaked until the entire process shuts down.
    This is why the two-phased constructor is introduced:
    - the "real" constructor (so the CSomething() ) is allowed to do anything which does not allocate resources (including memory). So it may contain "iTen=10", and similar lines, it can initialize member variables with values coming from the outer world, etc.
    - the second phase constructor is called ConstructL, and it is protected by the Cleanup Stack. It may contain memory allocations, initialization of R-objects
    However these things (invoking the constructor, and the ConstructL, the later protected with Cleanup Stack) do not happen automatically, and the wrapper method is the NewL, by convention:
    Code:
    CMobEyeThread *CMobEyeThread::NewL()
    {
        CMobEyeThread *diz=new (ELeave) CMobEyeThread(); // first phase
        CleanupStack::PushL(diz); // protect with Cleanup Stack - starting from now if anything happens, the Cleanup Stack contains the object pointer, so it can delete the partially constructed object
        diz->ConstructL(); // second phase
        CleanupStack::Pop(); // diz
        return diz; // return the constructed object
    }
    you can not invoke the constructor directly exactly because for C-classes it is protected by default (CActive has its constructor, and otherwise the parent class for all C-classes is CBase).
    Remarks:
    - new(ELeave) is nothing scary, it is a simple new operator, just instead of returning NULL, it drops a KErrNoMemory leave code
    - T-classes are value types, like TInt, TRect, TReal, etc. an active object should be named as CSomething, as a rule of thumb whenever you inherit from a C-class, the result is also a C-class
    - the multiply defined thing means what it says: the linker found multiple definitions of the given method. Make sure that each .cpp file is listed only once in the .mmp, and also keep method definitions (so the body) in the .cpp files, do not put them into the class declaration or the header file. The messages explicitly say "files MobEyeContainerView.o, MobEyeContainer.o", meaning that the definitions appear in (or included into) both MobEyeContainerView.cpp and MobEyeContainer.cpp.
    - undefined means it is not defined. DoCancel is an abstract method, you have to implement it yourself.
    Active objects are not exactly threads, check http://developer.nokia.com/Community...tive_Scheduler, it may give some ideas about how the entire thing operates.

  3. #3
    Registered User
    Join Date
    Jul 2013
    Posts
    73

    Re: Inherited CActive class creation problem

    Thank you i made it. I have another question, why i can't read data from this class. (i tried to draw a string that is made in this class) but i see no text

  4. #4
    Nokia Developer Moderator
    Join Date
    Mar 2003
    Location
    Lempäälä/Finland
    Posts
    29,166

    Re: Inherited CActive class creation problem

    How are you trying to do the drawing ? Basically CActive classes are not having GUI parts, thus you are likely doing the drawing with something else..

  5. #5
    Registered User
    Join Date
    Jul 2013
    Posts
    73

    Re: Inherited CActive class creation problem

    In the CActive class
    AnsiString k = "sample text";
    then in drawing function:
    gc.DrawText(*PC_EYE->k.AnsiStringToHBufLC(),TPoint(30,150));
    CleanupStack::PopAndDestroy();

    where

    (in AnsiString structure)
    HBufC *AnsiStringToHBufLC()
    {

    int i;
    TUint8 *aString = new TUint8[len+1];

    for (i=0; i < len; i++) aString[i] = text[i];
    aString[len] = NULL;
    TPtrC8 str((const TUint8*)(aString));



    HBufC *ret=HBufC::NewLC(str.Length());
    ret->Des().Copy(str);
    delete aString;
    return ret;
    }

    ofc
    TMobEyeThread * PC_EYE;


    since i write not for PC i think i wil have to make some extra conditions.

    Even making AnsiStringToHBufLC() function outside CActive class (made different function that isn't returning a pointer from a CActive doesn't work.
    Last edited by melepeta; 2013-10-11 at 18:19.

  6. #6
    Nokia Developer Moderator
    Join Date
    Mar 2003
    Location
    Lempäälä/Finland
    Posts
    29,166

    Re: Inherited CActive class creation problem

    Firstly the Draw() fucntion in the CCoeControl is non-leaving, secondly its called loads of times, so please do not do any non-needed processing in the function. If you need to construct any HBufC or other variables, do them outseide the Draw function. Also do check what gets put into your HBufC variable, likely you are having problems on the conversion.

  7. #7
    Registered User
    Join Date
    Jul 2013
    Posts
    73

    Re: Inherited CActive class creation problem

    Yes, somehow i can retreive integer from the CActive but can't get this structure (AnsiString)


    Another issue is that ConstructL() does not execute. i get p = 0 instead of p = 1 ;x

    Code:
    void TMobEyeThread::ConstructL()
        {                             
    	k = "sample text";
    p = 1;
        }
     
    TMobEyeThread* TMobEyeThread::NewL()
        {
    	  TMobEyeThread *diz=new (ELeave) TMobEyeThread(); // first phase
    	    CleanupStack::PushL(diz);
    	    diz->ConstructL();
    	    CleanupStack::Pop(); 
    	    return diz; 
        }

  8. #8
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,699

    Re: Inherited CActive class creation problem

    An obvious thing to make sure is that you really create the object with NewL, and not with the simple new.

  9. #9
    Registered User
    Join Date
    Jul 2013
    Posts
    73

    Re: Inherited CActive class creation problem

    lol :P hehe that solved my problem :]

Similar Threads

  1. Replies: 29
    Last Post: 2012-01-23, 11:27
  2. Replies: 0
    Last Post: 2011-02-22, 05:30
  3. Cannot compile inherited class! Please help!
    By slickire in forum Symbian
    Replies: 6
    Last Post: 2009-06-24, 10:45
  4. Replies: 5
    Last Post: 2007-12-14, 13:57
  5. Replies: 2
    Last Post: 2006-08-30, 21:33

Posting Permissions

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