You don’t need to keep track of the reference count yourself. FltMgr
manages the reference counting of the context internally. Your
responsibility is to ensure that when you reference the context, you
subsequently de-reference it. There have been many threads on here
about how to do this properly. The most confusing aspect of this is
understanding when you need to release the context when you allocate and
set it. Within the last month or two, somebody posted a nice
explanation of how that might work internally, and Alex confirmed that
yes, you may think of it that way but the particulars are subject to
change.
Pragmatically speaking, you probably don’t need to keep a count to
handle this properly. Maintaining a pointer should be sufficient unless
you’re doing something weird.
~Eric
A couple points on your code follow:
I maintain a ref_count=0 intiialized in PostCreate().
If FltGetStreamContext() passes, increment ref_count.
conceptually correct, if you call FltGetXxxContext, you need to release
the context afterwards
Else go to cleanup:
If FltSetStreamContext() passes, increment ref_count.
What context would you be setting here? You haven’t allocated a new one
yet. The only context you have is the one you just got.
Else go to cleanup:
If FltAllocateContext() passes, increment ref_count.
If you allocate context, you do need to release it. If you then try to
set a newly allocated context, you need to release it whether the call
to FltSetXxxContext succeeds or fails. If the Set call succeeds, FltMgr
(in essence) adds its own reference to the context which it releases
when the object is disposed of. If it fails, FltMgr does not. In
either case, you still hold a reference to the context that you need to
release.
else go to cleanup:
Cleanup:
for (i=0; i<ref_count i>> if (context != NULL)
> FltReleaseContext(Context);
></ref_count>