Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

casting CPropertyPage

Posted on 2011-09-20
10
Medium Priority
?
329 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 35

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
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
LVL 35

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 35

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 35

Accepted Solution

by:
sarabande earned 2000 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

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!

Question has a verified solution.

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

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

610 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