This blog is about developing Windows applications using Visual Studio. All postings on this weblog are provided "AS IS" with no warranties, and confer no rights. Use of any samples are subject to the terms specified at http://www.microsoft.com/info/cpyright.htm
Your host Nikola Dudar is a Program Manager in Windows division of Microsoft Corporation. He has been working on Windows Web Services API during Windows 7 and various additions to Visual C++ during VS2005 and VS2008. More details are in LinkedIn profile under Nikola's formal name Mykola Dudar.
If you are interested in program management and project management, check out my other blog at http://www.pmsnack.com/ where I collect best practices and other topics interesting to program and project managers.
To send feedback, comments or requests for new posts, please use the contact form.
I came across an interesting discussion today. A person who asked the question called a method of C++ object after it was deleted. And it worked. I guess if I have ever attempted to do something like this, it failed for me and I have not had a chance to ask this question. Here is the code snippet (copying from email thread):
ref class Foo
{
public:
~Foo() { Console::WriteLine(L"Foo::~Foo"); }
void Doit() { Console::WriteLine(L"Foo::Doit"); }
};
class Bar
~Bar() { Console::WriteLine(L"Bar::~Bar"); }
void Doit() { Console::WriteLine(L"Bar::Doit"); }
int main(array<System::String ^> ^args)
Foo^ f = gcnew Foo();
f->Doit();
delete f;
f->Doit(); // This works fine.
Bar* b = new Bar();
b->Doit();
delete b;
b->Doit(); // This also works fine.
return 0;
}
Apparently the reason for why this works is that even the compiler maps the call to delete to a call to the Dispose/destructor of the object, the garbage collector may not immediately reclaim the memory. In result, it is still possible to access the managed object. Why does this work for native code? Because in this example the code of the method does not access memory or use any data member of the object. If it was, then it would fail because memory has been already released at that point of time. An example of this would be iterating though a collection of data stored in a member variable of this class. Overall behavior in this scenario (after delete is called) is undefined and anything can happen. In case of a person who asked this question, it just worked fine. And I thank him for asking good question!