CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1

    pointer to pointer to heap

    I'm not sure about this:

    Does this work?
    Code:
    classA::classA(){
      classA1 pa1 = new classA1;
      classA2 pa2 = new classA2(pa1);
    }
    
    classA2::classA2(classA1* p){
      m_pa1 = p; //Member-Variable
    }
    
    Later, I create a Thread that gets a pointer to m_pa1 and uses this pointer even after pa2 is destroyed.
    
    threadFunctionBla(..){
      classA2* pa2 = //get out of parameter//;
      classA1* pa1 = pa2->m_pa1;
      //do somwthing.
      //classA2 is detroyed now by another Thread
      pa1->doSomething(); //does this work? I get strange results.
    i now it's confusing. if anybody sees any errors or wants to show a btter way, i'd be glad.

  2. #2
    Join Date
    Oct 2002
    Location
    Arkansas, USA
    Posts
    189
    Code:
    classA2* pa2 = //get out of parameter//;
      classA1* pa1 = pa2->m_pa1;
      //do somwthing.
      //classA2 is detroyed now by another Thread
      pa1->doSomething(); //does this work? I get strange results
    This is very hard to follow. You use the same names for the variables in the definition for ClassA and threadFunctionBla(..).
    If i am following correctly, you seem to say that the pa2 variable in threadFunctionBla(..) really ends up pointing to an invalid address because of the statement:
    //classA2 is detroyed now by another Thread
    If this is the case, the call:
    Code:
    pa1->doSomething(); //does this work?
    can't "work" properly, since pa2->m_pa1 is also now an invalid address. (classA1* pa1 = pa2->m_pa1)
    Last edited by gjs368; October 22nd, 2003 at 09:55 AM.

  3. #3
    Join Date
    Feb 2003
    Location
    Brazil
    Posts
    335

    Use semaphores

    A simple way to work around this is using semaphores, setting a global variable indicating if the object is already destroyed or not.

    the following code is an exemple of this thecnic (not tested)

    Code:
    :	
    	
    class A1
    {
      ...
      int * Semaphore;
      A1(int * PSemaphore);
      ~A1();
      ...
    }
    
    A1::A1(int* PSemafhore):Semaphore(PSemaphore)
    {
      ...
     *Semaphore = 1 ;    //--- Semaphore sinalizes if A1 is alive.
      ...
    };
    
    A1::~A1()
    {
       *Semaphore = 0;    //--- A1 is destroyed....
    }
    
    class A2
    {
       ...
       int * A1Alive;
       A2(int* PA1Alive);   // on construction A1 recieves the  semaphore
       ...
       inline bool CheckA1Alive() { return *A1Alive == true;};
    
       public:
      
      void UseA1();
    }
    
    A2::A2(int * PA1Alive):A1Alive(PA1Alive){}; 
     
    void A2::UseA1()
    {
    
      if (CheckA1Alive)
      {
         // work with A1...
      }
      else
      {
         // A1 is destroyed, nothing to do...
      }
    }
    
    main()
    {
      int Semaphore = 0;
    	
      A1(&Semaphore);  
    	
      ...
    
      A2(&Semaphore);  // A2 needs to get ride of the semaphore
      A2.UseA1();	
      ...
    :

    

  4. #4
    Join Date
    Aug 2002
    Posts
    58
    can't "work" properly, since pa2->m_pa1 is also now an invalid address.
    Not neccessarily true.

    Dereferencing pa2 will naturally fail, but:

    Code:
    classA1* pA1 = pA2->m_pA1;
    delete pA2;
    pA1->DoSomething();
    can work fine, provided the destructor for classA2 doesn't explicitly delete m_pA1.

    Regards,
    Bassman

  5. #5
    thx bassmann.
    no, classA2 does not delete m_pA1. But it may set
    m_pA1 = 0;
    Would this be a problem

  6. #6
    Join Date
    Aug 2002
    Posts
    58
    No, as long as you call

    Code:
    pA1 = pA2->m_pA1;
    before

    Code:
    pA2->m_pA1 = 0;
    executes (obviously). Setting the pointer to 0 or even to another valid value won't delete the object that it points to.

    Regards,
    Bassman

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured