Solved

casting CPropertyPage

Posted on 2011-09-20
10
325 Views
Last Modified: 2012-08-13
Hi Experts,

I'm doing the following:
CPropertyPage * pActivePage = m_pMyPropSheet->GetActivePage();
((CMyDerviedPropPageClass*)pActivePage)->DoSomething();

When I look at the address of pActivePage on the 2nd line (before entering DoSomething), it's different than the "this" pointer that I get within DoSomething (i.e., I'm casting pActivePage to what it's supposed to be, but when I do and I go in this class, it's pointing at something else!

How is this possible?

Thank you!
Mike
0
Comment
Question by:thready
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
10 Comments
 
LVL 1

Author Comment

by:thready
ID: 36569622
Note that when I place the mouse over pActivePage in DEBUG mode, the compiler knows that it's a CMyDerivedPropPageClass*...  But isn't the "this" pointer supposed to be equal to pActivePage's address when I'm in one of the functions of the class?
0
 
LVL 1

Author Comment

by:thready
ID: 36571598
Very confusing, I hope I'm being clear enough...
0
 
LVL 34

Expert Comment

by:sarabande
ID: 36572174
that could happen when you did mult-inheritance like

class CMyDerviedPropPageClass : public CMyOtherPropPageClass, public CPropertyPage
{

then the pointer of CMyDerviedPropPageClass definitively was different cause only the first baseclass would/could share the same pointer with a derived class object.

generally the compiler is free to use different pointers though it is unlikely if no multi-inheritance was involved.

Sara
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 34

Expert Comment

by:sarabande
ID: 36572199
note, when inheritance each pointer has an associated virtual pointer table where the pointers to each class (part) are stored.

in case of a cast the compiler can choose the right pointer.

if you would do

CMyDerviedPropPageClass* pActivePage =
     (CMyDerviedPropPageClass*)m_pMyPropSheet->GetActivePage();

the pActivePage would be same as the 'this' in DoSomething.

Sara
0
 
LVL 1

Author Comment

by:thready
ID: 36574093
Hi Sara, thank you for your answers- I tried what you said and I was convinced you were correct.  Now my pActivePage is the same value as the original page I added to the property sheet as the address of the object passed to AddPage as expected.  However, when I step into "DoSomething", the this pointer is still not the same address as that of pActivePage.  Something very fishy is going on here I think....  Could this be because of a stray pointer or something?

Thanks again!
Mike
0
 
LVL 34

Expert Comment

by:sarabande
ID: 36574310
i don't think, but i already had doubts whether it could be because of multi-inheritance cause mfc could not handle multi-inheritance correctly.

you should verify that the 'this' you looked on really is in

CMyDerivedPropPageClass::DoSomething

and not in a function called because of the arguments to DoSomething.

you could examine the stack for that.

Sara

0
 
LVL 1

Author Comment

by:thready
ID: 36574388
DoSomething doesn`t even have any arguments unfortunately.  I wish it would have been that!  

Mike
0
 
LVL 1

Author Comment

by:thready
ID: 36575434
It looks like all this is not the reason for my problem.  I'm getting an access violation on bad data.  It looks like a memory overrun.  I have a member variable whose address changes (and definitely should not).  It's an object inside my property page (created as an instance variable, not newed).  The address of this object changes when I go through some steps in the code.  I'd like the program to break right when the contents of the address changes.  There must be a way to do that (to break from anywhere in the program at the current line as soon as the contents of that memory is altered)?

Thank you!
Mike
0
 
LVL 34

Accepted Solution

by:
sarabande earned 500 total points
ID: 36579464
yes, an access violation is an indication for corrupted memory. then the 'this' shown in the debugger is not really reliable. are you absolutely sure that the active page you get from property sheet really IS a  CMyDerivedPropPageClass. if not both access violation and wrong this probably could be explained though a wrong cast should return a null pointer actually.

you should verify that by checking explicit

  CMyDerivedPropPageClass* p =  dynamic_cast<CMyDerivedPropPageClass*>(pActivePage).

that definitively must give a null pointer if the page is not right class.

if you still have the same misery after that, the corruption already happened before and probably has destroyed virtual pointer tables. you should examine the stack then and check for other variables as well. when memory is corrupted also other pointers and objects both on stack and heap could show starnge values in the debugger.

Sara
0
 
LVL 1

Author Closing Comment

by:thready
ID: 36926558
Thanks!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question