|
Hi,
In a com component ,I took a dialog resource added a class
for it and even inserted an Activex Control on dialog
(with activex variable m_ctrlHMFAx).
I am creating the dialog dynamically as shown below
pDialog = new CMyDialog();
if(pDialog != NULL)
{
BOOL ret = pDialog->Create(IDD_MYDIALOG,this);
if(!ret)
AfxMessageBox("Error creating Dialog");
pDialog->ShowWindow(SW_SHOW);
}
Now as my dialog is created I am trying to work with my
ActiveX variable as pDialog->m_ctrlHMFAx.AddItem(); but
it throws an assertion
when I debug I recognized that m_ctrlHMFAx is showing NULL
value.I tried creating the activex control dynamically in
OnInitdialog but no use.
I also placed AfxEnableControlContainer() in initinstance.
What could be the reason ? Any suggestions would be helpful.
Thanks
Satya
Today is a gift, that's why it is called the present.
|
|
|
|
|
narayanagvs wrote: I tried creating the activex control dynamically in
OnInitdialog but no use.
Usually you can have a quit amount of info (from HRESULT return value) whenever a step of such a operation fails.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|
narayanagvs wrote: BOOL ret = pDialog->Create(IDD_MYDIALOG,this);
if(!ret) //Create failed.
AfxMessageBox("Error creating Dialog");
pDialog->ShowWindow(SW_SHOW);
If Create() fails, why proceed with calling ShowWindow() ?
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
narayanagvs wrote:
BOOL ret = pDialog->Create(IDD_MYDIALOG,this);
if(!ret) //Create failed.
AfxMessageBox("Error creating Dialog");
pDialog->ShowWindow(SW_SHOW);
You must use of
if(!ret)
AfxMessageBox("Error creating Dialog");
else
pDialog->ShowWindow(SW_SHOW);
|
|
|
|
|
Hello everyone,
This question is about when it is allowed to call throw to re-throw exception -- make the exception re-throw.
I can only think of two situations,
1. in catch block of an exception;
2. in unexpected handler.
For (2), in unexpected handler, since the input parameter is null, so in order to get the exception information, we need to re-throw it and catch it to get exception informaiton.
Is my understanding correct? Here is some pseudo code from Bjarne's book.
void throwY() throw (Yunexpected)
{
try{
throw;
} catch (Network_exception& p)
{
throw (Yunexpected (&p));
} catch (...)
{
throw (Yunexpected (0));
}
}
thanks in advance,
George
|
|
|
|
|
Yes, and they're really the same as the unexpected handler is just like an ultimate catch block for when all others have failed to catch. If you want to understand how this works in the real world, as opposed to how Bjarne says it should work, then take a look at the source code for the C Runtime that ships with Visual Studio. C++ exception handling in the MS world relies on both the runtime and special code injected into your functions by the Compiler. There's also a good article on here somewhere called something like How C++ Exception Handling Really Works. Enjoy
Nothing is exactly what it seems but everything with seems can be unpicked.
|
|
|
|
|
Hi Matthew,
I can not find the article you mentioned "How C++ Exception Handling Really Works" from Google. Could you provide a link please?
regards,
George
|
|
|
|
|
My mistake George the title is somewhat different. This[^] is the one I'd seen a link to. It comes up every now and then in the list of related popular articles at the bottom of one page or another on CP. If you soak up all that you'll know more about exception handling than I do so you can answer the next query that comes up in that area
Nothing is exactly what it seems but everything with seems can be unpicked.
|
|
|
|
|
Thanks Matthew,
Great article! About my original question, do you agree?
--------------------
For (2), in unexpected handler, since the input parameter is null, so in order to get the exception information, we need to re-throw it and catch it to get exception informaiton.
Is my understanding correct?
--------------------
regards,
George
|
|
|
|
|
Hello everyone,
The following swap technique is used to make assignment operator exception safe (means even if there is exception, the current object instance's state is invariant).
It used a temporary object "temp" in this sample, and assignment is made on a to temp ar first. Even if there is exception, the current this object's state is not corrupted.
My question is, the pattern works only if there is no exception thrown by swap function. If there are exception in swap function, the state of current object instance may still be corrupted (swap may invoke the assignment operator of member variables). Is my understanding correct?
class A;
A& A::operator= (const A& a)
{
A temp;
temp = a;
swap (*this, temp);
return *this;
}
thanks in advance,
George
|
|
|
|
|
Hey George, wait a moment, throw out your book, that's recursive junk!!!!!!!!!!!
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|
Why CPallini? More information please?
regards,
George
|
|
|
|
|
George you're a quite smart guy: I think you'll soon reckon than calling the equal operator inside its very definition causes a theoretical endless function call loop and a practical stack overflow.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|
Hi CPallini,
I agree and I think it depends on how swap is implemented. If swap is implemented by assignments of POD types, I think it is safe.
regards,
George
|
|
|
|
|
Nope, my friend, the line
temp = a;
causes infinite recursion no matter how swap is implemented.
BTW could you please post the original B. Stroustrup's sample (toxcct told me about... )?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|
Hi CPallini,
This is not Bjarne's sample.
This question is what I learned that swap could be used to design and implement an exception safe assignment operator. Yes, there is some issues in my code.
I think you are well known about how to use swap to implement exception safe assignment opeator from your replies. Could you help to correct my code please?
regards,
George
|
|
|
|
|
George_George wrote: This question is what I learned that swap could be used to design and implement an exception safe assignment operator.
Well, I think you learned about reading any book (or article). Please reference it, if you can.
George_George wrote: I think you are well known about how to use swap to implement exception safe assignment opeator from your replies. Could you help to correct my code please?
In fact, no. I've grasped the main idea from your sample code, but I wasn't aware of that technique, hence I cannot try to fix the code simply guessing about (if it is a well established pattern, then we need a state-of-art example)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|
Thanks for your comment, CPallini!
Let us wait for other MVP's reply.
regards,
George
|
|
|
|
|
George_George wrote: Let us wait for other MVP's reply.
And, of course, for replies of any other member.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|
Yes. but I believe that the state of the parameter only may throw an exception, if its state is corrupted.
that's why we do a temporary assignement (temp = a ) to check first if assigning a to something else would not crash.
so if a is OK, then swapping it with this would be OK too...
|
|
|
|
|
Well, even ignoring the recursion madness of George sample code, I think that swap may take care about, since it will perhaps be coded in a way similar to the following:
A temp;
temp = a;
a = *this;
*this = temp;
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|
that's how swap is normally designed.
however, there's a lack in this design, because having a A::operator=() calling a A::swap() function, which itself calls A::operator=() 3 more times is recursive for sure, with no possibility to stop the loop.
so I think swap is not coded like this in B. Stroustrup's example...
|
|
|
|
|
toxcct wrote: that's how swap is normally designed.
The above I was suggesting: hence no need to create a temporary assignment before it.
toxcct wrote: however, there's a lack in this design, because having a A::operator=() calling a A::swap() function, which itself calls A::operator=() 3 more times is recursive for sure, with no possibility to stop the loop.
so I think swap is not coded like this in B. Stroustrup's example...
I agree that there is recursion in the sample code (after all I was the first pointing out it...) and I was not suggesting to take that code as it stands, suppose, for instance, that method name was foo , instead of operator== .
BTW I think that swap should rely on the assignment operator and not the opposite (but I've not Stroustrup's example, that of course I suppose correct, at hand, I think George is not a diligent poster recently... )
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|
Thanks CPallini,
I agree that there is some issue in my code. It is appreciated if you could provide some pseudo code to correct my code to implement the using swap technique to implement a exception safe assignment operator.
regards,
George
|
|
|
|
|
George_George wrote: I agree that there is some issue in my code.
Some issue? Some issue? Some issue?
I won't call 'some issue' going into infinite recursion while aiming to provide an 'exception safe' operator.
Just kidding...
George_George wrote: It is appreciated if you could provide some pseudo code to correct my code to implement the using swap technique to implement a exception safe assignment operator.
Oh no, you made the original post: that's your side of the game.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|