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. C++ - Accessibility of object after deletion. (illegal still working), why this is happening?

C++ - Accessibility of object after deletion. (illegal still working), why this is happening?

Scheduled Pinned Locked Moved C / C++ / MFC
c++tutorialquestion
4 Posts 3 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.
  • S Offline
    S Offline
    Satish Shankar Jagtap
    wrote on last edited by
    #1

    In my project I created object for class using new operator. After use I deleted that object and set that to NULL. But even after deletion, I could able to access and able to run other functions using that (deleted)object. Please go through example: CTemp *objTemp = new CTemp; objTemp->Fun1(); delete objTemp; objTemp = NULL; objTemp->Fun2(); // It is illegal still works, why? Why this is happening?

    CPalliniC J 2 Replies Last reply
    0
    • S Satish Shankar Jagtap

      In my project I created object for class using new operator. After use I deleted that object and set that to NULL. But even after deletion, I could able to access and able to run other functions using that (deleted)object. Please go through example: CTemp *objTemp = new CTemp; objTemp->Fun1(); delete objTemp; objTemp = NULL; objTemp->Fun2(); // It is illegal still works, why? Why this is happening?

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      Because the method is generated for the class, not for the instance of the class (that is you don't delete code).

      In testa che avete, signor di Ceprano?

      1 Reply Last reply
      0
      • S Satish Shankar Jagtap

        In my project I created object for class using new operator. After use I deleted that object and set that to NULL. But even after deletion, I could able to access and able to run other functions using that (deleted)object. Please go through example: CTemp *objTemp = new CTemp; objTemp->Fun1(); delete objTemp; objTemp = NULL; objTemp->Fun2(); // It is illegal still works, why? Why this is happening?

        J Offline
        J Offline
        Jochen Arndt
        wrote on last edited by
        #3

        In addition to the answer by CPallini: It will only work when your CTemp::Fun2() function does not access non-static member variables or call other member functions that would do so. Then the implicit this pointer will be used which is NULL generating an access violation.

        CPalliniC 1 Reply Last reply
        0
        • J Jochen Arndt

          In addition to the answer by CPallini: It will only work when your CTemp::Fun2() function does not access non-static member variables or call other member functions that would do so. Then the implicit this pointer will be used which is NULL generating an access violation.

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          Of course. :thumbsup:

          In testa che avete, signor di Ceprano?

          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