constructL related query.

Login to reply to this topic.
Thu, 2008-07-10 18:47
Joined: 2006-12-18
Forum posts: 42

In two phase constructor is it mandatory to call ConstructL method ??
can we not put the code inside ConstructL in NewL method. If this happens we dn't need to ocall constructL method.


Thu, 2008-07-10 21:59
Joined: 2006-05-08
Forum posts: 162
Re: constructL related query.

No its not mandetory. Its only done to keep things simple and clear.

Which would you like?

foo* foo::newl()
{
foo* f = new (ELeave)foo;
CleanupStack::PushL(foo);
f->iBuf =  HBufC::NewL(10);
f->iData = CData::NewL();
CleanupStack::Pop(f);
return f;
}

or

foo* foo::newl()
{
foo* f = new (ELeave)foo;
CleanupStack::PushL(foo);
f->ConstructL();
return f;
}


Fri, 2008-07-11 04:45
Joined: 2006-12-18
Forum posts: 42
Re: constructL related query.

Thanks friend.........if this is the case but i have some doubt over here.
Basically newL method is static so it can access only static variables. Objects which are not static can't be called from NewL or NewLC .
In my openion this is the reason we call ConstructL method.

I am not very sure about it . So you folks have some light on it.

Tue, 2008-07-15 12:53
Joined: 2008-06-23
Forum posts: 85
Re: constructL related query.

ConstructL is normally non-static, so that cannot be the reason for calling it. (But since NewL is a member in the class it instantiates, it can also access all other class members and member functions of an instance of its class, and not only static ones. )

The actual reasons are more likely convenience, increased readability and coding conventions.

Thu, 2008-07-17 05:52
Joined: 2007-01-17
Forum posts: 99
Re: constructL related query.

Actually Using Construct() in two phase construction is very much necessary. In ConstructL() you should actually initialize your code not in the constructor., the reason bieng:
You push the Object on to the clean up stack after construction, then initialize class members in ConstructL(), if by any remote chance ConstructL() leaves .,the object then is popped out of cleanupstack and deleted


Shashi Kiran G M

Thu, 2008-07-17 06:45
Joined: 2008-06-23
Forum posts: 85
Re: constructL related query.

gmsk19, you are absolutely right, but the question was different Eye-wink

Thu, 2008-07-17 07:28
Joined: 2005-11-20
Forum posts: 1241
Re: constructL related query.

Yes, the point that is in danger to get lost in confusion, but which maxxxpayne mentioned already is the following: While two-phase construction is something of a technical necessity, ConstructL is mererly a convention. Still a very good idea to follow it of course, but there is no technical reason that strictly dictates exactly where you finish construction of your objects.

Just out of curiosity, I tried to find out how things look over in the Windows Mobile C++ camp. They must have the same problem, or at least had it, because I guess that new things are programmed mostly in C# with the help of the .NET Compact Framework, which works with automatic garbage collection and therefore mostly avoids memory leak problems.

Interestingly enough, I got the impression that there is no generally accepted and mandated equivalent to Symbian's two-phase construction in Windows Mobile C++ programming. One hint that this is so I found in this article about porting Symbian programs to Windows Mobile which goes to great lengths explaining those C++ programmers how two-phase construction works and tries to convince them that the problem it solves is real:
http://msdn.microsoft.com/en-us/library/aa454903.aspx


René Brunner

Fri, 2008-07-18 20:34
Joined: 2007-09-23
Forum posts: 159
Re: constructL related query.

"Actually Using Construct() in two phase construction is very much necessary. In ConstructL() you should actually initialize your code not in the constructor., the reason bieng:
You push the Object on to the clean up stack after construction, then initialize class members in ConstructL(), if by any remote chance ConstructL() leaves .,the object then is popped out of cleanupstack and deleted"

So in that case where is the potential memory leak in the code not using ConstructL() that maxxxpayne posted?

  • Login to reply to this topic.