Re: Class Destructor doesn't destroy?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Just for fun, here's an even more esoteric solution :)

char storage[sizeof(Numbers)];
Numbers* n2 = new(storage) Numbers(5, 6);
....
n2->~Numbers();

The above code allocates and subsequently constructs your
object on the stack, then destroys it when you are finished and
finally the memory is reclamed when storage goes out of scope.
Note that unlike automatic variables, the destructor won't be called
automatically.

Now, of course I won't actually recommend this kind of coding
style, but in rare specialized circumstances I've found the placement
new technique useful... (When writing a custom memory manager
to be more precise.)

--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: agnickolov@xxxxxxxx
MVP VC FAQ: http://vcfaq.mvps.org
=====================================

"Gerry Hickman" <gerry666uk@xxxxxxxxxxxxxxxx> wrote in message
news:uER06v1XIHA.536@xxxxxxxxxxxxxxxxxxxxxxx
Hi,

If I instantiate a Class using operator new, I can destroy it with
operator delete, but if I instantiate it without operator new, I don't
seem to be able to destroy it until it goes out of scope - manually
calling the destructor doesn't completely destroy it.

In this function, the destructor gets called twice; once in-line, then
again when the function returns.

void classInstantiation() {
int a,b;

Numbers n2 = Numbers(5,6);
a = n2.Add();
wcout << a << endl; // prints 11
b = n2.Multiply();
wcout << b << endl; // prints 30

// Finished with numbers now
n2.~Numbers();

// Work with strings
// More processing here

}

--
Gerry Hickman (London UK)


.



Relevant Pages

  • Class Destructor doesnt destroy?
    ... If I instantiate a Class using operator new, I can destroy it with operator delete, but if I instantiate it without operator new, I don't seem to be able to destroy it until it goes out of scope - manually calling the destructor doesn't completely destroy it. ... Gerry Hickman ...
    (microsoft.public.vc.language)
  • Re: Class Destructor doesnt destroy?
    ... If I instantiate a Class using operator new, I can destroy it with operator delete, but if I instantiate it without operator new, I don't seem to be able to destroy it until it goes out of scope - manually calling the destructor doesn't completely destroy it. ...
    (microsoft.public.vc.language)
  • Re: Dopey Delphi
    ... Destroy, ... The destructor releases the memory. ... FreeAndNil which can hide some nasty bugs). ... there is quite a lot of that in Delphi by now. ...
    (comp.lang.pascal.delphi.misc)
  • Re: Multiple destructors?
    ... and it seemed to me that destroying a file object might be a ... > Destroy would just destroy the list, ... For it is the destructor called by Free. ... Actually, on checking, all virtual methods in TObject are in the ...
    (comp.lang.pascal.delphi.misc)
  • Re: CoDisconnectObject and VB
    ... destroy the object if it has other clients. ... Microsoft MVP, MCSD ... Right now when this is done, the server object ... "Alexander Nickolov" wrote: ...
    (microsoft.public.win32.programmer.ole)