Solved

casting CPropertyPage

Posted on 2011-09-20
10
310 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
  • 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 32

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
 
LVL 32

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 32

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 32

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now