title: Facing an error "*** glibc detected *** free(): invalid next size (fast)"
body: <p>Please see MSO question <a href="https://meta.stackoverflow.com/questions/254782/a-long-list-of-possible-duplicates-c-memory-allocation-and-overrunning-bounds">A long list of possible duplicates — C memory allocation and overrunning bounds</a> for information about closely related questions.</p>
  
  <hr>
  
  <p>Developer environment: CentOS 4.7, Kdevelop 3.1.1, gcc 3.4.6</p>
  
  <p>I run a Java test client that loads a C++ shared library using JNI. There are three components in my application,</p>
  
  <ol>
  <li>Java client </li>
  <li>C++ shared library which acts as a JNI wrapper. (I will call it "wrapperlibrary")</li>
  <li>C++ shared library containing business objects. (I will call it "businesslibrary")</li>
  </ol>
  
  <p>When I run the client I face an error very frequently which is, <code>*** glibc detected *** free(): invalid next size (fast): 0x080eeef8 ***</code>. This error comes for around 10 - 11 times and then the application runs.</p>
  
  <p>In my Java client, I first load the required C++ libraries in a static ctor as follows,</p>
  
  <pre><code>static
  {
  System.Load("/root/Desktop/libs/businesslibrary");
  System.out.println("business library loaded");
  System.Load("/root/Desktop/libs/wrapperlibrary");
  System.out.println("wrapper library loaded");
  }
  </code></pre>
  
  <p>The statement "business library loaded" gets printed on the console but after it the error <code>*** glibc...</code> comes.</p>
  
  <p>In the project settings of wrapperlibrary, the businesslibrary is specified as a dependant library. So, even if I omit the call to load businesslibrary and just write,</p>
  
  <pre><code>static
  {
  System.Load("/root/Desktop/libs/wrapperlibrary");
  System.out.println("wrapper library loaded");
  }
  </code></pre>
  
  <p>then firstly the businesslibrary gets loaded(seen through global variable creation logging) and then the wrapperlibrary gets loaded. The control returns back to java client and the statement "wrapper library loaded" is printed on console. After this there is a call 
  to native method. But the control never reaches this native method's implementation. Rather before that the error <code>*** glibc...</code> again comes. Also if I insert a call to static method of another java class before native method call such as,</p>
  
  <pre><code>static
  {
   System.Load("/root/Desktop/libs/wrapperlibrary");
   System.out.println("wrapper library loaded");
   System.out.println(Try.temp()); //where temp is a static method of Try class which returns a string.
  
   native method call;
  
   --
   --
  }
  </code></pre>
  
  <p>then output of Try.temp() never gets printed.</p>
  
  <p>What could be the possible reasons for the problem in both these approaches and how should I proceed?</p>