Solved

casting CPropertyPage

Posted on 2011-09-20
10
316 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 33

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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
LVL 33

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 33

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 33

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Best book to learn C++ 4 79
Need more details 5 151
Visual Studio: built-in keystroke automation 2 65
How to install SVN Command Line Client? 5 66
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
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.

770 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