Click here to Skip to main content
15,914,010 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Yuck: Globals and Static Data Members Pin
alex.barylski26-Jan-02 22:03
alex.barylski26-Jan-02 22:03 
GeneralRe: Yuck: Globals and Static Data Members Pin
Swinefeaster26-Jan-02 22:21
Swinefeaster26-Jan-02 22:21 
GeneralWaitForSingleObject Pin
Sergei26-Jan-02 14:02
Sergei26-Jan-02 14:02 
GeneralRe: WaitForSingleObject Pin
Not Active26-Jan-02 16:22
mentorNot Active26-Jan-02 16:22 
Generalgrid lines Pin
A_choo26-Jan-02 13:36
A_choo26-Jan-02 13:36 
GeneralRe: grid lines Pin
alex.barylski26-Jan-02 22:23
alex.barylski26-Jan-02 22:23 
GeneralRe: grid lines Pin
A_choo29-Jan-02 22:45
A_choo29-Jan-02 22:45 
GeneralPointer Validation dynamic_cast<> Pin
Swinefeaster26-Jan-02 12:32
Swinefeaster26-Jan-02 12:32 
Hi guys,

I need some expert advice here, if anyone knows more about this stuff Wink | ;) .

First, let's name two classes for clarity. cService and cUser.

The cService class uses a handle mechanism to give the cUser class a way to indicate which agreggate objects (within cService) are being referred to whenever it makes various function calls. That handle, as far as the cUser is concerned, is just a number which means nothing to the cUser. But internally to cService, this handle is really a pointer to a link element in a linked list.

The problem that arises is that sometimes the cUser may have an invalid handle because of multithreaded (de)synchronization, which it will pass to the cService. The cService class needs to check whether this handle is valid, so what I have devised is to cast the handle to some base class pointer, namely cDestructible*, and then try to do cast it to the templated link list element class pointer, say cListLink<cServiceAggregate>*.

This is done through a safe_dynamic_cast<>, which is basically a dynamic_cast<> wrapped in try catch handlers, as so:

<code>
// safe_dynamic_cast<> never throws an exception; it traps all exceptions, and
// returns a NULL pointer instead (and so the cast must be between two
// pointers).
template <class tDestType, class tSourceType>
tDestType
safe_dynamic_cast(tSourceType SourceObject)
{
tDestType Dest = NULL;

try
{
Dest = dynamic_cast<tDestType>(SourceObject);
}
catch(...)
{
// Failed; return NULL.
}

return Dest;
}
</code>

Now this mechanism seems to work fine. To further clarify (if anyone has been able to follow so far), we have cListLink, which is derived from cDestructible. cService has a list of cServiceAggregate objects which are referred to by the cUser via handles. When the cUser object calls a function of the cService class, the cService class validates the handle, trying to convert the handle to a pointer, as follows:

<code>
cListLink<cServiceAggregate>* Aggregate =
safe_dynamic_cast<cListLink<cServiceAggregate>*>
((cDestructible*)handle);

if(Aggregate)
{
// Do stuff with Aggregate here, as it is valid.
}
</code>

Now this system seems to work fine in debug mode, and has been working quite well for a while actually. But in release mode something falls apart. I get an access violation while in the dynamic_cast<> call, which is fine, but then my catch handler doesn't catch it---instead, everything just goes poop. The whole app crashes. Perhaps what I'm doing here is a no-no, but I don't know any other way I can do this.

C++ support RTTI, which I find is useless, because there is no way of determining if a given pointer is of a given type (only if it's MOST DERIVED type is that type). I can't just use a base class and a virtual function cause this doesn't help as we don't know if the handles are valid.

Anyone have any thoughts to this?

Cheers!

swinefeaster

Check out Aephid Photokeeper, the powerful digital
photo album solution at www.aephid.com.

GeneralRe: Pointer Validation dynamic_cast<> Pin
Christian Graus26-Jan-02 13:33
protectorChristian Graus26-Jan-02 13:33 
GeneralRe: Pointer Validation dynamic_cast<> Pin
Swinefeaster26-Jan-02 13:37
Swinefeaster26-Jan-02 13:37 
GeneralRe: Pointer Validation dynamic_cast<> Pin
Christian Graus26-Jan-02 13:41
protectorChristian Graus26-Jan-02 13:41 
GeneralRe: Pointer Validation dynamic_cast<> Pin
Swinefeaster26-Jan-02 13:56
Swinefeaster26-Jan-02 13:56 
GeneralRe: Pointer Validation dynamic_cast<> Pin
26-Jan-02 15:25
suss26-Jan-02 15:25 
GeneralRe: Pointer Validation dynamic_cast<> Pin
Swinefeaster26-Jan-02 16:08
Swinefeaster26-Jan-02 16:08 
GeneralRe: Pointer Validation dynamic_cast<> Pin
Andrew Peace27-Jan-02 0:50
Andrew Peace27-Jan-02 0:50 
GeneralRe: Pointer Validation dynamic_cast<> Pin
Swinefeaster27-Jan-02 0:51
Swinefeaster27-Jan-02 0:51 
GeneralRe: Pointer Validation dynamic_cast<> Pin
Jamie Hale28-Jan-02 5:17
Jamie Hale28-Jan-02 5:17 
GeneralRe: Pointer Validation dynamic_cast<> Pin
Swinefeaster28-Jan-02 9:32
Swinefeaster28-Jan-02 9:32 
GeneralRe: Pointer Validation dynamic_cast<> Pin
Matt Gullett26-Jan-02 14:25
Matt Gullett26-Jan-02 14:25 
GeneralRe: Pointer Validation dynamic_cast<> Pin
Swinefeaster26-Jan-02 16:05
Swinefeaster26-Jan-02 16:05 
GeneralRe: Pointer Validation dynamic_cast<> Pin
26-Jan-02 16:45
suss26-Jan-02 16:45 
GeneralRe: Pointer Validation dynamic_cast<> Pin
Swinefeaster26-Jan-02 16:54
Swinefeaster26-Jan-02 16:54 
GeneralRe: Pointer Validation dynamic_cast<> Pin
Tim Smith26-Jan-02 17:13
Tim Smith26-Jan-02 17:13 
GeneralRe: Pointer Validation dynamic_cast<> Pin
Tim Smith26-Jan-02 17:17
Tim Smith26-Jan-02 17:17 
GeneralRe: Pointer Validation dynamic_cast<> Pin
Tim Smith26-Jan-02 17:09
Tim Smith26-Jan-02 17:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.