locked
Memory managment in ISAPI filters RRS feed

  • Question

  • User463357442 posted
    We are developing couple of ISAPI filters and not sure how it is expected to manage memory. We need to share some data between IIS notifications for one request. AllocMem function is not acceptible because memory is freed only on EndOfSession stage and not reused, so memory usage may grow dramatically.
    Currently we are allocating memory and keeping a list of allocated contexts bounded to the client IP+PORT which identifies actual client connection. So memory is allocated once for entire session and then reused for every request.
    The problem is how to free the memory. We have tried to free it on a EndOfSession notification, but sometimes (when session expires by timeout) client IP and PORT values are undefined. Connecion_ID variable is marked obsolete in MSDN so we don't use it. Do we have to build some king of garbage collector in addition to freing memory on EndOFSession event? I hope there should be some other way to manage memory in ISAPI filters. Can somebody help me with it?
    Monday, September 11, 2006 11:01 AM

All replies

  • User-75227868 posted

    I assume you are trying "..to share some data between IIS notifications for one request..." within a filter. More specifically, lets say you have written MyISAPIFilter.dll which has registered for 3 different notifications and for every individual http request, you want to pass some information between these three notifications within MyISAPIFilter.dll and that information will be specific to that individual request.

    If thats what you want you could use HTTP_FILTER_CONTEXT::pFilterContext to store a pointer to your custom data and this pointer will be available to your filter on each of the three notifications. Also, pFilterContext will be specific to a request so two simultaneous requests (but in different sessions) will see a different value of pFilterContext - the one that they had set during the beginning of their request processing. Hopefully, this should address the "where to store my request specific data" question.

    And from your scenarios description, looks like you *should* be using AllocMem(). You are right that memory allocated by AllocMem will be freed on EndOfSession but I am not sure why you feel that that memory cannot be reused between two requests in the same session? If you save your memory pointer to pFilterContext on your first request, that memory is available across multiple filter-notifications and throught the session. And since the second request (on the same session - if keepalive) will come after the end of the first request you should be able to simply use pFilterContext to get the memory pointer and reuse it for your current request. You will no  longer have to worry about freeing this memory yourself since it automatically gets deleted on end of session.

    Ref: http://msdn.microsoft.com/library/en-us/wcecomm5/html/wce50lrfhttpfiltercontext.asp?frame=true
    Note: Alloc mem will only be freed on EndoFSession.
    Note: If keepalive enabled, a session could have > 1 request but request2 will start after request1 completes.

    Below is an attempt to describe in a better(hopefully!) way what I explained:

    SessionA-Request=1:
    OnPreProc:pFilterContext IS NULL so pFilterContext=allocmem(MAX_OF_WHAT_MY_APP_NEEDS); store notif&req specific data "I am for Session1RQ1 weather=sunny"
    OnNotif2: pFilterContext should have "I am for Session1RQ1 weather=sunny"; update notif&req specific data "I am for Session1RQ1 weather=cloudy"
    onNotif3: pFilterContext should have "I am for Session1RQ1 weather=cloudy"; update notif&req specific data "I am for Session1RQ1 weather=rainy"
    Request1-EndoFRequest

    SessionA-Request=2:
    OnPreProc:pFilterContext!=NULL so don't allocate; store notif&req specific data "I am for Session1RQ2 weather=sunny"
    OnNotif2: pFilterContext should have "I am for Session1RQ2 weather=sunny"; update notif&req specific data "I am for Session1RQ2 weather=cloudy"
    onNotif3: pFilterContext should have "I am for Session1RQ2 weather=cloudy"; update notif&req specific data "I am for Session1RQ2 weather=rainy"
    Request2-EndoFRequest

    SessionA-EndOfSession: Memory pointed by pFilterContext will automatically be freed

    If this answers your query, great. If not, I might not have understood your specific case completely and more info would help.

    Wednesday, September 20, 2006 8:43 PM