How do I release the memory of a LPWSTR?

           LPWSTR pszAuxClass[]={L"top",L"symark-UserContext"};

Thank you.
lapuccaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
Not at all - these are constant string literals in an array, the compiler will take care of the cleanup. Udually, they're even placed in a 'const' data segment, so you even can't do that.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MysidiaCommented:
With that declaration, you don't.

You can only release memory explicitly during runtime that you actually allocated during runtime, such
as with the new operator.

In case you use the new operator,  there is a corresponding  delete  operator to release.

Automatic variables are released when their lifetime ends,  I.E.  if they're defined inside a block, when
the block exits, or the function returns,  or if they're global: when the program exits.

But string literals like   "symark-UserContext"     are global.
AxterCommented:
>>LPWSTR pszAuxClass[]={L"top",L"symark-UserContext"};

I recommend that you use a constant pointer when ever you declare a pointer to a string literal.
Example:
LPCWSTR pszAuxClass[]={L"top",L"symark-UserContext"};

This will prevent you from accidently trying to modify a string literal.
itsmeandnobodyelseCommented:
>>>> How do I release the memory of a LPWSTR

if you would have

  LPWSTR lpwsz = new WCHAR[20];
  wcscpy(lpwsz, L"ABCDEFGHI");

  ...

you would need to release by

  delete [] lpwsz;
 
after use.

If you have

 LPWSTR* lpwszArr = new LPWSTR[10];
 for (int i = 0; i < sizeof(lpwszArr)/sizeof(LPWSTR); ++i)
 {
       LPCWSTR prefix = L"file";
     
       wcscpy(lpwszArr[i], prefix);
       WCHAR wc = L'0' + (WCHAR)i;
       wcsncat( lpwszArr[i], &wc, 1);
  }
         
you would release allocation by
 
   for (i = 0; i < sizeof(lpwszArr)/sizeof(LPWSTR); ++i)
   {
        delete[] lpwszArr[i];
    }
    delete[] lpwszArr;

Generally, you have to delete if - and only if - you have made a new before.

Regards, Alex

itsmeandnobodyelseCommented:
correction:

LPWSTR* lpwszArr = new LPWSTR[10];
 for (int i = 0; i < 10; ++i)  
 {
       LPCWSTR prefix = L"file";
       lpwszArr[i] = new WCHAR[sizeof("file")+2];
       wcscpy(lpwszArr[i], prefix);
       WCHAR wc = L'0' + (WCHAR)i;
       wcsncat( lpwszArr[i], &wc, 1);
  }
         
you would release allocation by
 
   for (i = 0; i < 10; ++i)
   {
        delete[] lpwszArr[i];
    }
    delete[] lpwszArr;


It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.