Flaw in C++ 2005 compiler?

After setting a ref type = nullptr, I should not be able to reference the object, but this is not the case.  Even after GC::Collect, the object is still there.  What on earth is going on

#pragma once

using namespace System;

ref class Managed

{

public:

Managed();

~Managed();

!Managed();

void Write();

};

#include "stdafx.h"

#include "Managed.h"

Managed::Managed()

{

}

Managed::~Managed()

{

Console::WriteLine("Managed destructor has been called.");

this->!Managed();

}

Managed::!Managed()

{

Console::WriteLine("Managed finalizer has been called.");

}

void Managed::Write()

{

Console::WriteLine("Managed object still exists!");

}

#include "stdafx.h"

#include "Managed.h"

using namespace System;

int main(array<System::String ^> ^args)

{

// Any old managed object.

Managed ^m_Man = gcnew Managed();

// Run destructor.

// delete m_Man;

// Object still there.

m_Man->Write();

// Remove reference.

m_Man = nullptr;

// Object destroyed .

GC::Collect();

// Test it.  Still there!

m_Man->Write();

Console::WriteLine("End Of Test.");

return 0;

}

 

 

 

 

 

 




Answer this question

Flaw in C++ 2005 compiler?

  • BhavikS

    Could you post a complete piece of code that illustrates this How are you making your interpretation that "the object is still there "

    Brian


  • akqajohn

    The reason why the program runs and the object 'appears' to still exist is that your code never uses the this-pointer inside of the method - so Write acts as if it was a static member function. I changed you program by adding a non-static data member to Managed and having Write print out the value of this non-static data member. Now when I run the program I get:

    Managed object still exists! - 42
    Managed finalizer has been called.

    Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
       at Managed.Write()
       at main(String[] args)
       at mainCRTStartupStrArray(String[] arguments)

    Showing that the object really doesn't exist.



  • nrs251

    So, here is the dumb question of the day: why does "act" as if it were a static function if it not a static function

  • FiftyFeet

    It acts as a static member function because the this-pointer is unused - as soon as the this-pointer is used the runtime detects that it is null and throws the appropriate exception.

    In C++ the difference between a static member function and an instance member function is that the instance member function has an extra, hidden, parameter which is a pointer to the instance of the object on which this member function is currently operating. If the this-pointer is never referenced then you could make the instance member function static with no noticably change in your program.

    Note: this is different to C# - in C# all instance member functions are called virtually (even if they are non-virtual functons) this forces the CLR to check that the this-pointer is not null. So in C# you cannot call an instance member function with a null this-pointer.



  • Flaw in C++ 2005 compiler?