views:

94

answers:

5

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 delete base64 at the end? Does delete deallocates everything used by base64?

+1  A: 

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.

Georg Fritzsche
+4  A: 

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.

DeadMG
A: 

base64 is allocated on the stack and will be destroyed as soon as it leaves scope. There is no need to delete anything here.

pmr
+3  A: 

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.

Kristo
+2  A: 

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.

Tristram Gräbener
+1 even only for "kittens will die" :D
Matteo Italia