Hi Jeff,
Thanks for the response! : )
Ok, got a few questions then. How do I "force garbage collection"? Don't
know exactly what 'stack semantics' are (in contrast to the code sample I
provided), could you provide an equivalent to my code that IS in stack
semantic form?
Are you saying that the resources for the object previously pointed to by
'instance' are still 'valid' (even though there is no way to get to it from
the code since no pointers to it exist anymore) until the 'garbage
collector' decides to start garbage collecting, at which point the resources
may be released if needed?
The big question is as follows: Is there any call to a class that indicates
that it has just become elligible for garbage collection (that is, no
pointers exist that point to it)? That is, something that would trigger in
the sample code I gave when the only (or last) pointer to the object is set
to nullptr?
[==P==]
[quoted text, click to view] "JAL" <JAL@discussions.microsoft.com> wrote in message
news:E53EB1D2-363E-46EC-9C62-AEC6E3861846@microsoft.com...
> Peter... When a managed class is no longer reachable from root it is
> eligible
> for garbage collection. You can force the garbage collector just as a test
> case to force the call to Finalize. In your example you are not using
> stack
> based semantics so the destructor will not be called. I wrote an artice
> here
> that may be of help.
>
>
http://www.geocities.com/jeff_louie/deterministic_destructors.htm >
> Jeff
>
> "Peter Oliphant" wrote:
>
>> I would assume in the following that 'instance' being set to 'nullptr'
>> should cause the instance of myClass that was created to no longer have
>> any
>> pointers pointing to it, and therefore be 'destroyed':
>>
>> ref myClass
>> {
>> public:
>> myClass() {}
>> ~myClass() {} // never called
>> !myClass() {} // never called
>> } ;
>>
>> main()
>> {
>> myClass^ instance = gcnew myClass() ;
>> instance = nullptr ; // shouldn't this cause destruction?
>> }
>>
>> However, as indicated, neither the destructor or finalizer get called
>> when
>> 'instance' is set to 'nullptr'. Is the object previously pointed to by
>> 'instance' still around (.e., it still has memory resources)? Isn't
>> having
>> all existing pointers to an object either being set to nullptr or going
>> out
>> of scope cause the object to be destructed? If not, what am I missing
>> here?
>> If so, why is neither the destructor or finalizer called?
>>
>> For context, using VS C++.NET 2005 Express in /clr mode, I'm trying to
>> count
>> (using a static class variable) the number of existing instances of
>> myClass.
>> It is easy to count the creations (bump counter in each constructor), but
>> how do I count the destructions?
>>
>> thanks in advance for responses! : )
>>
>> [==P==]
>>
>>
>>