Why there is not error (or warning)?

C

Carlos

Hi,

in VSC++2005

I have a ref class

ref class A
{
public:
// ...
int n;
double d;
};

int main()
{
A^ pA = gcnew A();
pA->n = 33;
pA->d = 3.1416;
Console::WriteLine( "{0} {1}" , pA->n , pA->d ); // OK
delete pA;
Console::WriteLine( "{0} {1}" , pA->n , pA->d ); // OK!!??

return 0;
}

Why I do not have an error (or a warning) in the 2nd call to
Console::WriteLine ?
Why I have the right values of n and d after calling delete?

Thanks.
 
B

Bruno van Dooren [MVP VC++]

in VSC++2005
I have a ref class

ref class A
{
public:
// ...
int n;
double d;
};

int main()
{
A^ pA = gcnew A();
pA->n = 33;
pA->d = 3.1416;
Console::WriteLine( "{0} {1}" , pA->n , pA->d ); // OK
delete pA;
Console::WriteLine( "{0} {1}" , pA->n , pA->d ); // OK!!??

return 0;
}

Why I do not have an error (or a warning) in the 2nd call to
Console::WriteLine ?

Because the compiler does not check if your code is buggy.
It only checks if the syntax is correct.
(except for simple things like uninitialized values etc)
Why I have the right values of n and d after calling delete?
Because the garbage collector has not yet reclaimed and reused the memory
your object occupied at that point.

--

Kind regards,
Bruno van Dooren
(e-mail address removed)
Remove only "_nos_pam"
 
C

Carlos

Bruno van Dooren said:
Because the compiler does not check if your code is buggy.
It only checks if the syntax is correct.
(except for simple things like uninitialized values etc)

Because the garbage collector has not yet reclaimed and reused the memory
your object occupied at that point.

--

Kind regards,
Bruno van Dooren
(e-mail address removed)
Remove only "_nos_pam"

Thank you,

and if I write this:

// ...
delete pA;
pA = nullptr;
// ...

Would be any problem with the garbage collector?

Thanks again.
 
?

=?iso-8859-1?Q?Kim=20Gr=e4sman?=

Hi carlos,
and if I write this:

// ...
delete pA;
pA = nullptr;
// ...
Would be any problem with the garbage collector?

What do you mean by "problem"?

I think you are confused on what the delete keyword does in C++/CLI. It no
longer reclaims allocated memory, but rather calls the deleted object's IDisposable::Dispose
implementation, if there is one. Read more here: http://www.bluebytesoftware.com/blog/PermaLink.aspx?guid=88e62cdf-5919-4ac7-bc33-20c06ae539ae
(long, but good)

Whether you set the object reference to nullptr or not really has nothing
to do with the garbage collector. The GC can discover that the pA object
is no longer referenced from anywhere ("rooted") and will have its memory
reclaimed.
 
C

Carlos

Kim Gräsman escribió:
Hi carlos,



What do you mean by "problem"?

I think you are confused on what the delete keyword does in C++/CLI. It
no longer reclaims allocated memory, but rather calls the deleted
object's IDisposable::Dispose implementation, if there is one. Read more
here:
http://www.bluebytesoftware.com/blog/PermaLink.aspx?guid=88e62cdf-5919-4ac7-bc33-20c06ae539ae
(long, but good)

Whether you set the object reference to nullptr or not really has
nothing to do with the garbage collector. The GC can discover that the
pA object is no longer referenced from anywhere ("rooted") and will have
its memory reclaimed.

Thanks, now all is more clear.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top