Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Class Destruct

Class Destruct

Scheduled Pinned Locked Moved C / C++ / MFC
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Peter Liddle
    wrote on last edited by
    #1

    Hey Guys If i use an instance of a class in a funtion then deallocate it is it deallocated again when the function drops out. I only ask cos i keep getting a close handle again on my file access class at the end of my function even though i have already destructed the instance and haven't created another. Peter

    C R M 3 Replies Last reply
    0
    • P Peter Liddle

      Hey Guys If i use an instance of a class in a funtion then deallocate it is it deallocated again when the function drops out. I only ask cos i keep getting a close handle again on my file access class at the end of my function even though i have already destructed the instance and haven't created another. Peter

      C Offline
      C Offline
      Chris Losinger
      wrote on last edited by
      #2

      if you "new" an object, then the destructor is only called when you "delete" it. if you allocate the object on the stack, the destructor is called when the object goes out of scope. one or the other (or you can explicitly call the destructor, but that's something you can't really do without knowing you're doing it) -c


      //The Mandelbrot set
      o(int O){putchar(O);}main(){float l[8],O,I=.05;char _;for(l[6]=15;l[6]<':';o
      (10),l[5]=-'$'*I+l[6]++*I)for(l[7]=-5;l[7]<'@';l[4]=-'('*I+l[7]++*I,o(_?'?':':'))for
      (*l=O=0,_=1;++_&&((l[2]=*l**l)+(l[3]=O*O)<4);O=*l*O+l[5]+O**l,*l=l[2]-l[3]+l[4]);}

      Smaller Animals Software

      1 Reply Last reply
      0
      • P Peter Liddle

        Hey Guys If i use an instance of a class in a funtion then deallocate it is it deallocated again when the function drops out. I only ask cos i keep getting a close handle again on my file access class at the end of my function even though i have already destructed the instance and haven't created another. Peter

        R Offline
        R Offline
        Ravi Bhavnani
        wrote on last edited by
        #3

        If you store a pointer to class A as member variable in class B, and class B is designed to delete the A object in its own destructor, you should reset the pointer to NULL when you manually delete object A. This will make it clear:

        B:: ~B()
        {
        // Free A
        if (m_ptrA != NULL) {
        delete m_ptrA;
        }
        }

        B::someFunction()
        {
        m_ptrA = new A();
        ...;
        delete m_ptrA;
        m_ptrA = NULL; // <--- Reset pointer after deleting object
        }

        /ravi "There is always one more bug..." http://www.ravib.com ravib@ravib.com

        1 Reply Last reply
        0
        • P Peter Liddle

          Hey Guys If i use an instance of a class in a funtion then deallocate it is it deallocated again when the function drops out. I only ask cos i keep getting a close handle again on my file access class at the end of my function even though i have already destructed the instance and haven't created another. Peter

          M Offline
          M Offline
          Mike Nordell
          wrote on last edited by
          #4

          No. But if you haven't created your own copy c'tor and assignment operator a HANDLE wrapper class might use the compiler generated versions and then two object might try to release the same resoucrce when they are destructed.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups