{"id":4021,"date":"2014-03-30T07:13:16","date_gmt":"2014-03-30T07:13:16","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/c-complex-memory-leak-collection-of-common-programming-errors\/"},"modified":"2014-03-30T07:13:16","modified_gmt":"2014-03-30T07:13:16","slug":"c-complex-memory-leak-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/c-complex-memory-leak-collection-of-common-programming-errors\/","title":{"rendered":"C complex memory leak-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m having a slight memory leak in my program and i&#8217;m not sure if it&#8217;s in my allocs or in the internal c structures. The only mallocs i&#8217;m using are these:<\/p>\n<pre><code>results = (int*) malloc (instance_n * sizeof (int) );\n\ninstances = (char**) malloc (instance_n * sizeof (char*) );\nfor (i = 0; i &lt; instance_n; i++) {\n  instances[i] = (char*) malloc (1001 * sizeof (char) );\n}\n\nList_add (); (standard doubly linked list. Never gave me a problem)\n<\/code><\/pre>\n<p>And i free everything in the same place:<\/p>\n<pre><code>free (results);\nList_clear (&amp;dynamic);\nfor (i = 0; i &lt; instance_n; i++) {\n  free (instances[i]);\n}\nfree (instances);\n<\/code><\/pre>\n<p>BTW: List_clear =<\/p>\n<pre><code>Node* node = list-&gt;last;\nif (node == NULL) return;\n\nwhile (node-&gt;previous != NULL)\n  {\n    node = node-&gt;previous;\n    free (node-&gt;next);\n  }\nfree (list-&gt;first);\n<\/code><\/pre>\n<p>Additionally, i&#8217;m using timeval and FILE structures (the files are fclosed at the end of the methods)<\/p>\n<p>Am I missing something? To me it most certainly looks like i&#8217;m freeing everything. I never had a memory leak problem before so I&#8217;m terrible at debugging it, but Valgrind keeps pointing out this memory leak:<\/p>\n<pre><code>==3180== HEAP SUMMARY:\n==3180==     in use at exit: 62,951 bytes in 361 blocks\n==3180==   total heap usage: 556 allocs, 195 frees, 115,749 bytes allocated\n==3180== \n==3180== LEAK SUMMARY:\n==3180==    definitely lost: 8,624 bytes in 14 blocks\n==3180==    indirectly lost: 1,168 bytes in 5 blocks\n==3180==      possibly lost: 4,925 bytes in 68 blocks\n==3180==    still reachable: 48,234 bytes in 274 blocks\n==3180==         suppressed: 0 bytes in 0 blocks\n==3180== Rerun with --leak-check=full to see details of leaked memory\n==3180== \n<\/code><\/pre>\n<p>I can&#8217;t help but notice the &#8220;14 blocks&#8221; part, but no part of my code allocates less than 20 parts and 8624 bytes is a multiple of 4 bytes so it&#8217;s most likely an integer leak.<\/p>\n<p>Thanks in advance<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m having a slight memory leak in my program and i&#8217;m not sure if it&#8217;s in my allocs or in the internal c structures. The only mallocs i&#8217;m using are these: results = (int*) malloc (instance_n * sizeof (int) ); instances = (char**) malloc (instance_n * sizeof (char*) ); for (i = 0; i &lt; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4021","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4021","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4021"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4021\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4021"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4021"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4021"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}