By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,419 Members | 1,179 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,419 IT Pros & Developers. It's quick & easy.

Pointer problems with graphics contexts

P: n/a
I'm developing a class which is to draw to a given graphics context i.e a
CClientDC in Visual C++. The problem I have it is that my functions implement a
specific interface which are platform independent and so do can't take in a
reference to a CClientDC in the parameter list.

As a result my class looks like:

class MSWinDisplayManager : public IDisplayManager
{
private:
CClientDC *winPointer;
public:
MSWinDisplayManager(CClientDC& win);
void drawPoint();
};

MSWinDisplayManager::MSWinDisplayManager(CClientDC & win)
{
winPointer = &win;
}

void MSWinDisplayManager::drawPoint()
{
COLORREF col;
col = RGB(0, 0, 255);
winPointer->SetPixel(10, 10, col);
}

The problem is that the user of this class may delete the CClientDC object that
winPointer points to:

CClientDC *winManager = new CClientDC(this);
MSWinDisplayManager winManager(*win);
Delete win;
winManager.draw(win);

How can my MSWinDisplayManager class detect that its winPointer variable is no
longer valid?

Many thanks.

Tristan.
Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"Tristan" <tr************@yahoo.co.uk> wrote in message
news:7f**************************@posting.google.c om...
I'm developing a class which is to draw to a given graphics context i.e a
CClientDC in Visual C++. The problem I have it is that my functions implement a specific interface which are platform independent and so do can't take in a reference to a CClientDC in the parameter list.

As a result my class looks like:

class MSWinDisplayManager : public IDisplayManager
{
private:
CClientDC *winPointer;
public:
MSWinDisplayManager(CClientDC& win);
void drawPoint();
};

MSWinDisplayManager::MSWinDisplayManager(CClientDC & win)
{
winPointer = &win;
}

void MSWinDisplayManager::drawPoint()
{
COLORREF col;
col = RGB(0, 0, 255);
winPointer->SetPixel(10, 10, col);
}

The problem is that the user of this class may delete the CClientDC object that winPointer points to:

CClientDC *winManager = new CClientDC(this);
MSWinDisplayManager winManager(*win);
Delete win;
winManager.draw(win);

How can my MSWinDisplayManager class detect that its winPointer variable is no longer valid?


It cannot, which is exactly the sort of reason you should not use pointers
in the first place. What you should do is have your MSWinDisplayManager take
a copy of the display context so no-one else can interfere with your private
copy.

This is off topic but I think one way to do this would be

class MSWinDisplayManager : public IDisplayManager
{
private:
CClientDC private_win; // not a pointer
public:
MSWinDisplayManager(CClientDC& win);
void drawPoint();
};

MSWinDisplayManager::MSWinDisplayManager(CClientDC & win)
{
private_win.Attach(win.Detach());
}

Effectively this transfers the underlying Win32 display context from win to
private_win, which means that the user could no longer use win to do
drawing, but they could delete it without any problems for you. AFAIK this
transfer of ownership is the best you can do, its not possible in the Win32
API to do a genuine copy of a display context.

john
Jul 19 '05 #2

P: n/a
Tristan wrote:
[SNIP]
CClientDC win(this);
win.Detach();

which results in the assertion failure, any way round this?


ASSERTION FAILED: please post to a Windows programming newsgroup instead of
comp.lang.c++. Thanx.

A
Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.