?
Solved

How do I release the memory of a LPWSTR?

Posted on 2006-04-26
5
Medium Priority
?
1,474 Views
Last Modified: 2009-07-29
           LPWSTR pszAuxClass[]={L"top",L"symark-UserContext"};

Thank you.
0
Comment
Question by:lapucca
5 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 1000 total points
ID: 16549648
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.
0
 
LVL 23

Expert Comment

by:Mysidia
ID: 16549844
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.
0
 
LVL 30

Expert Comment

by:Axter
ID: 16550186
>>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.
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 16552159
>>>> 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

0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 16552191
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;


0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question