MFC CView* delete question

Jan 8, 2011 at 5:37pm
Hello,

I would to ask about a pointer deletion problem.

I'm developing a SDI multiview application with MFC and I'm using to access at a view class from a child dialog.
To do this I'm using a casting in this way:

CView* v = ((CFrameWnd*) m_pMainWnd)->GetActiveView();

((MyView*)v)->MyFunction_in_Parent();


When I exit method must I delete *v pointer? Or set to null?
Any not CView pointer deletion will bring problems?

Thank you



Thank you
Jan 8, 2011 at 7:01pm
No on the delete - as you did not create the object at *v. It was already there - all you have done
is to get the address of it.


You could set the v pointer to NULL (v = NULL) - all depends on what you are
going to do with it next.
Jan 10, 2011 at 11:25am
I understand.
At last I want to delete that pointer, so I must just set it to NULL?

Jan 10, 2011 at 2:45pm
nio82 wrote:
When I exit method must I delete *v pointer? Or set to null?
Neither. v is a local variable that's not longer accessible after leaving scope (function) hence it makes no sense to assign null when it's not longer existent.

Imporant is that only that object that has the ownership of the pointer (called new) is supposed to call delete.

better use dynamic_cast<...*> so that you know that the object has the right type. dynamic_cast returns null if it's the wrong type
Topic archived. No new replies allowed.