?
Solved

casting CPropertyPage

Posted on 2011-09-20
10
Medium Priority
?
327 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

752 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