Click here to Skip to main content
15,919,422 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
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 
Actually that's exactly what I came up with after some more thought Wink | ;) . But then I ran into a global / static object problem with this scheme. Check out my more Recent Post... I implemented the code as follows:

<br />
#pragma once<br />
<br />
#include "cMap.h"<br />
#include "cCriticalSection.h"<br />
<br />
// The purpose of this class is to employ a way of checking whether a given<br />
// pointer or handle is actually a valid cParanoid pointer through the<br />
// use of a static thread safe map.<br />
<br />
class cParanoid : public cDestructible<br />
{<br />
   public:<br />
      cParanoid(void);<br />
      virtual ~cParanoid(void);<br />
<br />
       // Returns a cParanoid pointer if the passed Pointer is a valid <br />
       // cParanoid pointer; otherwise returns NULL.<br />
      static cParanoid* GetParanoid(void* Pointer);<br />
<br />
   private:<br />
       // A map of all the cParanoid objects that currently exist.<br />
      static cMap<cParanoid*, cParanoid*, bool, bool> m_ParanoidMap;<br />
<br />
       // Used to synchronize operations with the paranoid map.<br />
      static cCriticalSection m_ParanoidSection;<br />
};<br />
<br />
cMap<cParanoid*, cParanoid*, bool, bool> cParanoid::m_ParanoidMap;<br />
<br />
cCriticalSection cParanoid::m_ParanoidSection;<br />
<br />
cParanoid::cParanoid(void)<br />
{<br />
   m_ParanoidSection.Lock();<br />
   {<br />
      m_ParanoidMap[this] = true;<br />
   }<br />
   m_ParanoidSection.Unlock();<br />
}<br />
<br />
cParanoid::~cParanoid(void)<br />
{<br />
   m_ParanoidSection.Lock();<br />
   {<br />
      if(!m_ParanoidMap.RemoveKey(this))<br />
      {<br />
         // We weren't in the map, but that's ok as we may just be <br />
         // shutting down.<br />
      }<br />
   }<br />
   m_ParanoidSection.Unlock();<br />
}<br />
<br />
cParanoid* <br />
cParanoid::GetParanoid(void* Pointer)<br />
{<br />
   cParanoid* Paranoid = NULL;<br />
<br />
   m_ParanoidSection.Lock();<br />
   {<br />
      if(m_ParanoidMap.DoesKeyExist((cParanoid*)Pointer))<br />
      {<br />
         // It's valid.<br />
         Pointer = (cParanoid*)Pointer;<br />
      }<br />
   }<br />
   m_ParanoidSection.Unlock();<br />
<br />
   return Paranoid;<br />
}<br />


The problem is that I get a pure virtual function called upon shutdown of the app. This is because CWinApp is a global, and the critical section and map of cParanoid get destoyed before CWinApp does, and there are some objects owned by CWinApp that are derived from cParanoid. Frown | :(

Any thoughts?

Thanks!

swinefeaster

Check out Aephid Photokeeper, the powerful digital
photo album solution at www.aephid.com.
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 
GeneralRe: Pointer Validation dynamic_cast<> Pin
Swinefeaster26-Jan-02 20:26
Swinefeaster26-Jan-02 20:26 
GeneralRe: Pointer Validation dynamic_cast<> Pin
Swinefeaster26-Jan-02 21:32
Swinefeaster26-Jan-02 21:32 
GeneralRe: Pointer Validation dynamic_cast&lt;&gt; Pin
Swinefeaster17-Jul-03 10:05
Swinefeaster17-Jul-03 10:05 
GeneralWin98 vs WinNT Pin
Jim Crafton26-Jan-02 12:30
Jim Crafton26-Jan-02 12:30 
GeneralRe: Win98 vs WinNT Pin
567890123427-Jan-02 3:13
567890123427-Jan-02 3:13 
GeneralCode optimizations - script or compiled Pin
alex.barylski26-Jan-02 11:26
alex.barylski26-Jan-02 11:26 
GeneralRe: Code optimizations - script or compiled Pin
Rick York26-Jan-02 12:30
mveRick York26-Jan-02 12:30 
GeneralRe: Code optimizations - script or compiled Pin
alex.barylski26-Jan-02 13:01
alex.barylski26-Jan-02 13:01 
GeneralRe: Code optimizations - script or compiled Pin
markkuk27-Jan-02 19:56
markkuk27-Jan-02 19:56 
GeneralAccess violated Pin
Stephen Caldwell26-Jan-02 11:12
Stephen Caldwell26-Jan-02 11:12 
GeneralRe: Access violated Pin
Swinefeaster26-Jan-02 12:01
Swinefeaster26-Jan-02 12:01 
Generallocalhost Pin
Rickard Andersson2026-Jan-02 8:12
Rickard Andersson2026-Jan-02 8:12 

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.