Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory leak in CRFSuite::Tagger #73

Closed
nikolsky opened this issue Nov 2, 2016 · 3 comments
Closed

Memory leak in CRFSuite::Tagger #73

nikolsky opened this issue Nov 2, 2016 · 3 comments

Comments

@nikolsky
Copy link
Contributor

nikolsky commented Nov 2, 2016

I use crfsuite tagger in simple test program:

CRFSuite::Tagger* newTagger = new CRFSuite::Tagger();
newTagger->open("test_model.model");
...
tagVec = newTagger->tag(items);
delete newTagger;

Running the program in valgrind would return an error such as:

==11280== 7,519,904 bytes in 1 blocks are definitely lost in loss record 2 of 2
==11280==    at 0x4C2DBB6: malloc (vg_replace_malloc.c:299)
==11280==    by 0x42041A: crf1dm_new (in /home/artem/projects/test_crfsuite/dist/Debug/GNU-Linux/test_crfsuite)
==11280==    by 0x415B8F: crf1m_create_instance_from_file (in /home/artem/projects/test_crfsuite/dist/Debug/GNU-Linux/test_crfsuite)
==11280==    by 0x403551: CRFSuite::Tagger::open(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (crfsuite.hpp:289)
==11280==    by 0x403D1A: main (main.cpp:26)
==11280== 
==11280== LEAK SUMMARY:
==11280==    definitely lost: 7,519,904 bytes in 1 blocks
==11280==    indirectly lost: 0 bytes in 0 blocks
==11280==      possibly lost: 0 bytes in 0 blocks
==11280==    still reachable: 72,704 bytes in 1 blocks
==11280==         suppressed: 0 bytes in 0 blocks
==11280== 
==11280== For counts of detected and suppressed errors, rerun with: -v
==11280== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

"Definitely lost" here is equal .model file size.
The place that valgrind points:

...
buffer = buffer_orig = (uint8_t*)malloc(size + 16);
if (buffer_orig = NULL) {
    goto error_exit;
}
...
return crf1dm_new_impl(buffer_orig, buffer, size);

where:

static crf1dm_t* crf1dm_new_impl(uint8_t* buffer_orig, const uint8_t* buffer, uint32_t size)
{
...
model->buffer_orig = buffer_orig;

then in "destructor" memory will never free:

void crf1dc_delete(crf1d_context_t* ctx)
{
...
    if (model->buffer_orig != NULL) {
        free(model->buffer_orig);
        model->buffer_orig = NULL;
    }
    model->buffer = NULL;
...

After my fix #74 valgrind shows that memory doesn't leak any more:

==27439== LEAK SUMMARY:
==27439==    definitely lost: 0 bytes in 0 blocks
==27439==    indirectly lost: 0 bytes in 0 blocks
==27439==      possibly lost: 0 bytes in 0 blocks
==27439==    still reachable: 72,704 bytes in 1 blocks
==27439==         suppressed: 0 bytes in 0 blocks
==27439== 
==27439== For counts of detected and suppressed errors, rerun with: -v
==27439== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
@TrinhKien94
Copy link

TrinhKien94 commented Feb 20, 2017

I have same problem when i run crfsuite version 0.12:
==25384==
==25384== HEAP SUMMARY:
==25384== in use at exit: 1,320 bytes in 1 blocks
==25384== total heap usage: 1,139 allocs, 1,138 frees, 62,120,243 bytes allocated
==25384==
==25384== 1,320 bytes in 1 blocks are definitely lost in loss record 1 of 1
==25384== at 0x4C2FA50: calloc (vg_replace_malloc.c:711)
==25384== by 0x40E9A3: crf1dc_set_num_items (in /home/kl/gr/crfsuite-0.12/frontend/build/crfsuite)
==25384== by 0x40821B: tagger_set (in /home/kl/gr/crfsuite-0.12/frontend/build/crfsuite)
==25384== by 0x4053A9: tag (tag.c:321)
==25384== by 0x40584C: main_tag (tag.c:433)
==25384== by 0x404221: crfsuite (main.c:149)
==25384== by 0x40401F: main (main.c:105)
==25384==
==25384== LEAK SUMMARY:
==25384== definitely lost: 1,320 bytes in 1 blocks
==25384== indirectly lost: 0 bytes in 0 blocks
==25384== possibly lost: 0 bytes in 0 blocks
==25384== still reachable: 0 bytes in 0 blocks
==25384== suppressed: 0 bytes in 0 blocks
==25384==
==25384== For counts of detected and suppressed errors, rerun with: -v
==25384== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
Valgrind show that some thing in fucntion crf1dc_set_num_items have leak. Then i add this line: free(ctx->state); after line 99 (after line: free(ctx->alpha_score);) of file crfsuite-0.12/lib/crf/src/crf1d_context.c then i make && make install again. Run valgrind with my program again and have no leak as below:
==31489==
==31489== HEAP SUMMARY:
==31489== in use at exit: 0 bytes in 0 blocks
==31489== total heap usage: 1,139 allocs, 1,139 frees, 62,120,243 bytes allocated
==31489==
==31489== All heap blocks were freed -- no leaks are possible
==31489==
==31489== For counts of detected and suppressed errors, rerun with: -v
==31489== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
I think it's helpful for fix this issue.

@usptact
Copy link

usptact commented Feb 20, 2017

@chokkan bump!

@usptact
Copy link

usptact commented Feb 28, 2017

So this is is why we don't hear back from @chokkan :) He is playing soccer! :)
screen shot 2017-02-27 at 7 28 02 pm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants