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 sp_open? #47

Open
stephenmathieson opened this issue Mar 20, 2014 · 7 comments
Open

Memory leak in sp_open? #47

stephenmathieson opened this issue Mar 20, 2014 · 7 comments
Labels

Comments

@stephenmathieson
Copy link
Contributor

The example leaks 48 bytes (see this valgrind report):

#include <sophia.h>
#include <stdio.h>

int
main(void) {
  char *path = "/tmp/foodb";
  void *env = NULL;
  void *db = NULL;
  int rc = 0;

  if (!(env = sp_env())) {
    fprintf(stderr, "Failed to allocate environment\n");
    return 1;
  }

  rc = sp_ctl(env, SPDIR, SPO_CREAT|SPO_RDWR, path);
  if (-1 == rc) {
    sp_destroy(env);
    fprintf(stderr, "Failed to set database path\n");
    return 1;
  }

  rc = sp_ctl(env, SPGC, 1);
  if (-1 == rc) {
    sp_destroy(env);
    fprintf(stderr, "Failed to enable garbage collector\n");
    return 1;
  }

  if (!(db = sp_open(env))) {
    sp_destroy(env);
    fprintf(stderr, "Failed to open database\n");
    return 0;
  }

  printf("Opened database %s\n", path);

  sp_destroy(db);
  sp_destroy(env);
  return 0;
}

Is something wrong with the example, or is there a leak in Sophia?

@stephenmathieson
Copy link
Contributor Author

bump -- any thoughts?

@pmwkaa
Copy link
Owner

pmwkaa commented Apr 7, 2014

~/craft/sophia/test valgrind ./sophia-leak
==11039== Memcheck, a memory error detector
==11039== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==11039== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==11039== Command: ./sophia-leak
==11039==
Opened database /tmp/foodb
==11039==
==11039== HEAP SUMMARY:
==11039== in use at exit: 0 bytes in 0 blocks
==11039== total heap usage: 17 allocs, 17 frees, 67,160 bytes allocated
==11039==
==11039== All heap blocks were freed -- no leaks are possible
==11039==
==11039== For counts of detected and suppressed errors, rerun with: -v
==11039== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

~/craft/sophia/test valgrind --version
valgrind-3.7.0

That is funny, I might have outdated valgrind, i will check it up :)

@stephenmathieson
Copy link
Contributor Author

Very strange -- I've got the same version:

$ valgrind --version
valgrind-3.7.0

@pmwkaa
Copy link
Owner

pmwkaa commented Apr 7, 2014

There might be difference in pthread implementation (libc version), i need to checkout proper condition variable cleanup on exit. Thanks)

@stephenmathieson
Copy link
Contributor Author

Do you get the same successful results when running valgrind --leak-check=full ./sophia-leak?

@pmwkaa
Copy link
Owner

pmwkaa commented Apr 7, 2014

yeah, just rechecked. As far as i can see in source code, there are missed condition variable destruction on sp_destroy. Seems to be pthread implementation started to allocate memory for their own purposes, so the problem came up :)

@stephenmathieson
Copy link
Contributor Author

Interesting. If you point me in the right direction, I can try my hand at a fix :)

@pmwkaa pmwkaa added the v1.1 label Jan 30, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants