Should I call class destructor in this code?
I am using this sample to decode/encode some data I am retrieving/sending from/to a web server, and I want to use it like this:
BOOL HandleMessage(UINT uMsg,WPARAM wParam,LPARAM lParam,LRESULT* r)
{
if(uMsg == WM_DESTROY)
{
PostQuitMessage(0);
return TRUE;
}
else if(uMsg == WM_CREATE)
{
// Start timer
StartTimer();
return TRUE;
}
else if(uMsg == WM_TIMER)
{
//get data from server
char * test = "test data";
Base64 base64;
char *temp = base64.decode(test);
MessageBox(TEXT(temp), 0, 0);
}
}
The timer is set every 5 minutes.
Should I use del开发者_JAVA百科ete base64 at the end? Does delete deallocates everything used by base64?
Base64's destructor will do it automatically. Since Base64 exists on the stack, the destructor will be called for you. You don't need to manually release any of the resources in this code, except possibly "temp". However, you will need to check the Base64 documentation for this.
base64
is in local storage. It goes out of scope and is destructed at the end of the block. The only question left is ownership of temp
. If its memory is owned by base64
, then you do not need to delete
anything.
base64 will be automatically deleted at the end of the context. You can't delete it (if you delete &base64 kittens will die).
The destructor of base64 should delete everything it doesn't need anymore. Concerning temp, it depends on the lib. You have to check the documentation.
No, base64
is stack-allocated and the pointer returned by decode()
is to an array which is a data member of the class:
class Base64 {
// ...
char dst[MAX_LEN];
};
char* Base64::decode(char *src) {
// ...
return dst;
}
dst
will be automatically deallocated when the Base64
instance goes out of scope.
base64 is allocated on the stack and will be destroyed as soon as it leaves scope. There is no need to delete
anything here.
精彩评论