Who is responsible for deleting FILE_NOTIFY_INFORMATION.FileName?

198 Views Asked by At

Winapi's ReadDirectoryChanges uses FILE_NOTIFY_INFORMATION to present it's results. The struct looks like this:

typedef struct _FILE_NOTIFY_INFORMATION {
  DWORD NextEntryOffset;
  DWORD Action;
  DWORD FileNameLength;
  WCHAR FileName[1];
} FILE_NOTIFY_INFORMATION, *PFILE_NOTIFY_INFORMATION;

If I get this struct filled by winapi, how do I correctly delete the FileName WCHAR*? Do I have to delete it?

None of the examples (not that there are many examples) of the ReadDirectoryChanges mention deleting anything. Microsoft of course does not provide any examples at all.

1

There are 1 best solutions below

0
Rita Han On

If you use malloc you need call free after you finish using the object.

For example:

DWORD FileNameLength = 100;
PFILE_NOTIFY_INFORMATION file_notify_info = (PFILE_NOTIFY_INFORMATION)malloc(FIELD_OFFSET(FILE_NOTIFY_INFORMATION, FileName[FileNameLength]));

free(file_notify_info);

Refer to "Why do some structures end with an array of size 1?".