Note: you clearly need to read a book on introductory C programming! The
answer is obvious if you understand C. If you delete an instance of STRUCT,
NOTHING AT ALL WILL HAPPEN to any storage referenced by appPath. How could
it? Do you see anything that could possibly do this? Answer: no, because
there isn’t.
Now in C++, I could create a ~STRUCT() destructor that would call delete on
appPath, assuming that appPath was not shared with any other object, or I
could use smart pointers to manage the storage referenced by appPath so the
storage was freed when the last object referencing it was deleted. But
that’s C++, not C. In C, if you allocate it, you must free it; there is no
magic. It wouldn’t even make *sense* to have a free operation recursively
free everything pointed to by pointers in the structure! What you are
missing here is the concept of “ownership” of an object, that is, the
(abstract) owner is the person responsible for cleaning things up. In C,
the notion of ownership is based on what code you write, because it is a
very abstract concept that you are responsible for implementing correctly
(this is why so many C programs crash with bad memory references, because
the programmer has mismanaged what is going on). In C++, the concept of
ownership is also the responsibility of the programmer, but you can manage
it more coherently. Of course, no sane C++ programmer would ever use a
wchar_t * for anything, because std::wstring would be the mechanism of
choice.
But I think you are still missing the whole point of what is going on here.
If this is a structure you need to pass into the kernel, you cannot put
user-mode pointers in it, so forget it! If you need to fill in the path
from the kernel, the correct structure would be
typedef struct {
WCHAR appPath[MAX_PATH];
ULONG appPathSize;
} STRUCT, *PSTRUCT;
The structure you are showing makes no sense whatsoever for
application-to-kernel communications, unless you plan to do all pointer
validation yourself, and that is not something you want to consider! For
example, you have to validate the buffer address in the kernel, and in a
64-bit driver, that struct may be either a 64-bit WCHAR * or a 32-bit WCHAR
* (unless you do not allow 32-bit apps running under Win64 to call your
driver, and are prepared to detect and reject such connections).
Overall, your problems stem from a completely misplaced concept of
“efficiency” of storage, which is that you must manage variable-length
strings in a context where such strings provide no advantage, and in fact
provide significant disadvantage! If I were supervising this project, the
first thing I would have told you is that you have no idea what trouble this
will get you into, and just forget it. It would then be your responsibility
to convince me that saving a couple hundred bytes of 2GB (or 8TB) user space
justified the time, expense, and compromises to robustness that the pointer
solution would require. I think the whole design path you are following is
erroneous, and the sooner you abandon it, the better off you will be.
joe
-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of xxxxx@yahoo.com
Sent: Wednesday, July 20, 2011 11:50 AM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] Inverted Callback sending strings
Ok now I have a question about deallocation. Lets say I have the structure
below:
{
wchar_t* appPath;
int appPathSize;
}STRUCT, *PSTRUCT;
I allocate this structure. Then I allocate space for the appPath variable.
When I want to deallocate do I have to first deallocate the appPath variable
and then the structure? Or will deallocating the structure also deallocate
the appPath variable?
Ive been trying to deallocate the appPath and then deallocate the structure,
but I am getting the Bug Check 0x19: BAD_POOL_HEADER with the pool block
header size is corrupt.
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