Weird behavior: tests fail depending on how big the buffer is
<p>I have a very annoying bug in my code, and I was wondering if anyone here could help me track it down. Basically, I have written a wrapper around zlib's implementation of gzip, previously I had a function but I rewrote it as a class. I wrote some tests, but I keep on getting very weird errors. It seems like I get different errors depending on what size the class' internal buffer is, after some compiles I get a "Bus error", at others I the test fails at some point, always somwhere where it's testing cme::Inflate. </p>

<p>This is what I mean with the errors:</p>

<pre><code># ./bin/gzip_tests 
deflate test words
Bus error
</code></pre>

<p>This is an error where the code fails in the middle of a test:</p>

<pre><code>./bin/gzip_tests 
deflate test words
deflate test sentences
deflate test input
deflate test output
deflate test constructor
deflate test misc
inflate test words
Assertion failed: (inf.data() == "Monkey"), function inflate_test_words, file src/gzip_tests.cpp, line 147.
Abort trap
</code></pre>

<p>I have uploaded the three files in question to pastebin: <a href="http://pastebin.com/yidYsLU4" rel="nofollow" title="gzip.hpp">gzip.hpp</a>, <a href="http://pastebin.com/HDMuCxdr" rel="nofollow" title="gzip.cpp">gzip.cpp</a> and <a href="http://pastebin.com/NTBeYbyz" rel="nofollow" title="gzip_tests.cpp">gzip_tests.cpp</a>, I have been able to compile them fine with gcc version 4.2.1 (Apple Inc. build 5664) under Mac OS X 10.6 (Snow Leopard) with the following commands:</p>

<pre><code>g++ -c -Wall -Wno-sign-compare gzip.cpp -o gzip.o
g++ -c -Wall -Wno-sign-compare gzip_tests.cpp -o gzip_tests.o
g++ -lz -Wall -Wno-sign-compare gzip.o gzip_tests.o -o gzip_tests
</code></pre>

<p>Basically, the problem I have is that if I change</p>

<pre><code>static const int output_buffer_size = 372;
</code></pre>

<p>to anything else, like to 32768, I get different errors. Sometimes I get the aforementioned "Bus error", and I have really no idea as to why. Does anyone know what might be causing this?</p>

<p>Cheers and thanks in advance, xfbs</p>

<p><strong>EDIT</strong>: I have added the return value checks from the previous code, and now I'm a bit further: I know what error it is that zlib is throwing:</p>

<pre><code>deflate test words
deflate test sentences
deflate test input
deflate test output
deflate test constructor
deflate test misc
inflate test words
terminate called after throwing an instance of 'std::runtime_error'
  what():  Exception during zlib compression [write]: (-2) 
Abort trap
</code></pre>

<p>This still doesn't explain the Bus error I get when the buffer size is 32768. </p>

<p><strong>EDIT 2</strong>: Gosh I'm stupid... I figured out what the problem is. It's another one of those cases where I forgot to initialize variables because I am too lazy to finally learn C++. Anyway, in case anyone else runs into this, I believe the problem lies in the constructor, which was like this:</p>

<pre><code>Deflate::Deflate() {
  setup();
  compressionlevel = Z_BEST_COMPRESSION; // set it up with the best compression level
}
</code></pre>

<p>However, setup() needs to know the compressionlevel, but it's only set after setup is run, so setup is using an unititialized variable. Easily solved by doing this:</p>

<pre><code>Deflate::Deflate() {
  compressionlevel = Z_BEST_COMPRESSION; // set it up with the best compression level
  setup();
}
</code></pre>

<p>-xfbs</p>
