Pointer problems with graphics contexts

T

Tristan

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.
 
J

John Harrison

Tristan said:
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
 
A

Attila Feher

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top