nDPI icon indicating copy to clipboard operation
nDPI copied to clipboard

ndpiReader.c memory leak problem

Open u8906250 opened this issue 10 years ago • 15 comments

I think we need to free flow after ndpi_tdelete()

before

      while (ndpi_thread_info[thread_id].num_idle_flows > 0)
        ndpi_tdelete(ndpi_thread_info[thread_id].idle_flows[--ndpi_thread_info[thread_id].num_idle_flows],
                     &ndpi_thread_info[thread_id].ndpi_flows_root[ndpi_thread_info[thread_id].idle_scan_idx], node_cmp);

after

      while (ndpi_thread_info[thread_id].num_idle_flows > 0) {        
         ndpi_tdelete(ndpi_thread_info[thread_id].idle_flows[ndpi_thread_info[thread_id].num_idle_flows],
                     &ndpi_thread_info[thread_id].ndpi_flows_root[ndpi_thread_info[thread_id].idle_scan_idx], node_cmp);
         ndpi_free(ndpi_thread_info[thread_id].idle_flows[--ndpi_thread_info[thread_id].num_idle_flows])
    }

u8906250 avatar Feb 16 '16 04:02 u8906250

Thanks.

kYroL01 avatar Feb 16 '16 11:02 kYroL01

@kYroL01

you misunderstand what I mean, ha

need to free flow itself

like free(ndpi_thread_info[thread_id].idle_flows[--ndpi_thread_info[thread_id].num_idle_flows]); not free_ndpi_flow(ndpi_thread_info[thread_id].idle_flows[--ndpi_thread_info[thread_id].num_idle_flows])

u8906250 avatar Feb 17 '16 03:02 u8906250

Well... https://github.com/ntop/nDPI/blob/dev/example/ndpiReader.c#L593

An idle flow is a ndpi_flow and that function do the job. Maybe I must add ndpi_free(ndpi_thread_info[thread_id].idle_flows[--ndpi_thread_info[thread_id].num_idle_flows])

kYroL01 avatar Feb 17 '16 08:02 kYroL01

yes, I have tested it,

I watched the free memory was getting less and less if didn't free flow.

u8906250 avatar Feb 17 '16 09:02 u8906250

Yes I did a test too and I confirm what u say. But I also think that is necessary both functions. I push a commit.

kYroL01 avatar Feb 17 '16 09:02 kYroL01

I think you did call free_ndpi_flow() here

https://github.com/ntop/nDPI/blob/dev/example/ndpiReader.c#L687

no need to call again, ha

u8906250 avatar Feb 17 '16 09:02 u8906250

You have to understand that many part of the code are not written to us and are inherit from an old project. So I also need a better inspection of the code. I'm watching better and do a commit. Thanks for suggestion.

kYroL01 avatar Feb 17 '16 09:02 kYroL01

It's ok, you're welcome.

u8906250 avatar Feb 17 '16 09:02 u8906250

@kYroL01

sorry, have to remind you

--ndpi_thread_info[thread_id].num_idle_flows must be done after all stuff. otherwise you just free the next idle flow.

        ndpi_tdelete(ndpi_thread_info[thread_id].idle_flows[--ndpi_thread_info[thread_id].num_idle_flows], &ndpi_thread_info[thread_id].ndpi_flows_root[ndpi_thread_info[thread_id].idle_scan_idx], node_cmp);

    /* free the memory associated to idle flow in "idle_flows" - (see struct reader thread)*/
    free_ndpi_flow(ndpi_thread_info[thread_id].idle_flows[ndpi_thread_info[thread_id].num_idle_flows]);
    ndpi_free(ndpi_thread_info[thread_id].idle_flows[ndpi_thread_info[thread_id].num_idle_flows]);
      }

u8906250 avatar Feb 18 '16 02:02 u8906250

@u8906250 can you please send a pull request instead of bits and pieces. This would allow us to better evaluate your contribution. Thanks

lucaderi avatar Feb 18 '16 06:02 lucaderi

really! I try.

I never use pull request function before.

u8906250 avatar Feb 18 '16 06:02 u8906250

Oh, I was wrong

"--ndpi_thread_info[thread_id].num_idle_flows" is ok

I am used to put "--" at the rear like "ndpi_thread_info[thread_id].num_idle_flows--", so I get a little confused, ha

sorry about that

u8906250 avatar Feb 18 '16 06:02 u8906250

Exactly... is correct

kYroL01 avatar Feb 18 '16 11:02 kYroL01

Hello! Instead of using two functions "ndpi_flow_freer" and "ndpi_free" in one place, you can use already defined function "ndpi_flow_freer", that has already included these two functions. Logically, both variants are the same, but my variant little pretty.

mzhumakhanov avatar Mar 10 '16 15:03 mzhumakhanov

@mzhumakhanov I'm completely agree with you. Is a must-do-do thing that I need to change but soon a little refactory will be done. Thank you!

kYroL01 avatar Mar 10 '16 16:03 kYroL01