UNICODE_STRING when to free?

Again, sorry for these basic questions about UNICODE_STRING, I’m still learning here. Is there a hard and fast rule as to when it is required to free
the buffer of a UNICODE_STRING? For example, if I do this
UNICODE_STRING Filename;
RtlInitUnicodeString( &Filename, L"\??\" );
// play with Filename but not modify it

Do I need to free the buffer before exit?
Thanks again, Michael

The rule is simple: Free it when it is no longer referenced.

Thomas F. Divine


From: “Michael Wade”
Sent: Tuesday, January 04, 2011 10:08 AM
Newsgroups: ntdev
To: “Windows System Software Devs Interest List”
Subject: [ntdev] UNICODE_STRING when to free?

> Again, sorry for these basic questions about UNICODE_STRING, I’m still
> learning here. Is there a hard and fast rule as to when it is required to
> free
> the buffer of a UNICODE_STRING? For example, if I do this
> UNICODE_STRING Filename;
> RtlInitUnicodeString( &Filename, L"\??\" );
> // play with Filename but not modify it
>
> Do I need to free the buffer before exit?
> Thanks again, Michael
>
> —
> NTDEV is sponsored by OSR
>
> For our schedule of WDF, WDM, debugging and other seminars visit:
> http://www.osr.com/seminars
>
> To unsubscribe, visit the List Server section of OSR Online at
> http://www.osronline.com/page.cfm?name=ListServer

>Is there a hard and fast rule as to when it is required to free

the buffer of a UNICODE_STRING? For example, if I do this
UNICODE_STRING Filename;
RtlInitUnicodeString( &Filename, L"\??\" );

RtlInitUnicodeString doesn’t allocate any memory, it just sets up the fields
of the structure based on the wide character string provided. In this case
you don’t have to do anything to free the unicode string structure because
you’re using a static string, the structure will just go away when the stack
unwinds.

-scott


Scott Noone
Consulting Associate
OSR Open Systems Resources, Inc.
http://www.osronline.com

Hope to see you at the next OSR kernel debugging class February 14th in
Columbia, MD!

If you’re initializing a UNICODE_STING with a string literal you don’t even need to call RtlInitUnicodeString. Just use the RTL_CONSTANT_STRING macro which gives you the same result with less code:

const UNICODE_STRING Filename = RTL_CONSTANT_STRING(L"\??\");

-----Original Message-----
From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of Scott Noone
Sent: Tuesday, January 04, 2011 7:20 AM
To: Windows System Software Devs Interest List
Subject: Re:[ntdev] UNICODE_STRING when to free?

Is there a hard and fast rule as to when it is required to free the
buffer of a UNICODE_STRING? For example, if I do this
UNICODE_STRING Filename;
RtlInitUnicodeString( &Filename, L"\??\" );

RtlInitUnicodeString doesn’t allocate any memory, it just sets up the fields of the structure based on the wide character string provided. In this case you don’t have to do anything to free the unicode string structure because you’re using a static string, the structure will just go away when the stack unwinds.