×

Discussion Board

Results 1 to 2 of 2
  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    9

    A question about memory leak (from A Guide for Symbian C++ developers)

    class CElementsEngine : public CBase
    {
    private:
    CElementList* iElementList;
    CConsoleBase& iConsole;
    };

    CElementsEngine::~CElementsEngine()
    {
    delete iElementList;
    }



    What about the constructor? You might think this would suffice:
    CElementsEngine::CElementsEngine(CConsoleBase& aConsole)
    : iConsole (aConsole)
    {
    iElementList = new (ELeave) CElementList();
    }



    However, such constructors are not used in Symbian OS, and for good reason. To see why, you need to consider what would happen if the allocation of the new CElementList were to fail, causing a leave. Well, presumably this constructor would have been invoked elsewhere something like this:

    CElementsEngine* myEngine = new (ELeave) CElementsEngine(*console);


    Remember that this line does two important things: First, the new (ELeave) operator allocates memory for the new CElementsEngine instance (and all its nested data). Assume for the moment that this allocation is successful. Next it calls the C++ constructor shown above. If the allocation of the CElementList in the CElementsEngine constructor were to fail, say, due to running out of memory, there would be a problem. When this leave occurs, there is no pointer pointing to the area of memory successfully allocated for the CElementsEngine object, so this memory will be orphaned, and a memory leak will occur.


    how to lead a memory leak? I can't understand.
    if running out of memory, OS can't allocate any memory. Not to mention a memory leak.

  2. #2
    Super Contributor
    Join Date
    Jul 2007
    Posts
    596

    Re: A question about memory leak (from A Guide for Symbian C++ developers)

    But what abt the memory which have been sucessfully alocated for CElementsEngine. Who will free this memory if a leave occurs.

    Here i am explaining u in a simple exp
    Code:
    Class String:public CBase
    {
    public:
       string();
    private:
      char* str1;
      char* str2;
    };
    Code:
    string::string()
    {
      
      str1 = new char[10];// this allocates sucessfully //Who will free this memory
      str2 = new char[888888888]; // it fails and panic raises
    }
    [/code]
    Code:
    void main()
    {
      string* obj = new(ELeave) string(); // this line will panic
      //str1 = new char[10];// this allocates sucessfully //Who will free this memory now // who is the owner 
    }
    Therefore in symbian we use lightweight constructors. and make free this memory by cleanupstack methods.

    I suggest u to serch wiki for twophase construction and cleanupstack...

    Hope this will help to understand..
    Last edited by kishore84; 2009-05-21 at 13:03.

Similar Threads

  1. memory leak ! SDK bug or my fault ?
    By tfront in forum Symbian Tools & SDKs
    Replies: 3
    Last Post: 2007-11-12, 12:11
  2. Memory Leak, On device debugging.
    By jhsial in forum Mobile Java General
    Replies: 5
    Last Post: 2007-04-07, 10:44
  3. HTTP POST Memory Leak
    By WebSeed in forum Mobile Java Networking & Messaging & Security
    Replies: 1
    Last Post: 2006-12-08, 14:09
  4. severe memory leak when using CSendAppUi ( Nokia 7650 )
    By ejurinsky in forum Symbian Tools & SDKs
    Replies: 1
    Last Post: 2002-12-05, 10:13
  5. can not successfully link any sample using .NET
    By lobotomat in forum Symbian Tools & SDKs
    Replies: 2
    Last Post: 2002-08-20, 00:29

Posting Permissions

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