Stumped by non allocating class
Stumped by non allocating class
- Subject: Stumped by non allocating class
- From: Ken Tozier <email@hidden>
- Date: Thu, 20 Dec 2007 23:43:02 -0500
Hi
In my current project I have a class that doesn't appear to be
allocatable. What's happening is that using NSLog() right after
allocation prints (null) to the run log and stepping through the code
in the debugger, when I try to jump into the class on it's alloc
line, the debugger just ignores the command and skips right over it.
The class compiles without errors or warnings and I've checked the
class that uses it about 10 times to make sure I'm including the
header. (I am) I thought there might be invisible characters so I
copied the contents to BBEdit and did a "convert to ASCII" but no
unexpected characters appeared. Next I tried retyping the entire
method where it is used but that didn't work. I tried "Clean all
targets" several times, no luck. I quit XCode thinking maybe it was
in a weird state, no luck.
Anyone shed light on why a perfectly normal class won't allocate?
TIA
Ken
Here's the method where I'm having the problem
- (void) initWith8BitSampleData:(NSData *) inData
{
KHuffFileHeader header;
// next line is where I'm having the problem
KBitOutputStream *outStream = [KBitOutputStream bitOutputStream];
// next line prints (null) to the run log
NSLog(@"outStream: %@", outStream);
<snip>
}
And here's the KOutputBitStream interface and implementation
@interface KOutputBitStream : NSObject
{
unsigned int sizeInInts;
unsigned int sizeInBytes;
unsigned int sizeInBits;
unsigned int usedBits;
unsigned int nextBit;
unsigned int mask;
unsigned int *buffer;
unsigned int *s;
unsigned int *e;
}
+ (id) outputBitStream;
- (NSData *) data;
- (void) write:(int) inVal
bits:(int) inBits;
@end
#import "KOutputBitStream.h"
// define allocation block size of 65536 (2^16) bytes
#define KOUTPUT_BIT_STREAM_ALLOC_SIZE 16384
@implementation KOutputBitStream
+ (id) outputBitStream
{
return [[[KOutputBitStream alloc]
init]
autorelease];
}
- (id) init
{
self = [super init];
if (self)
{
buffer = malloc(KOUTPUT_BIT_STREAM_ALLOC_SIZE * sizeof(int));
s = buffer;
e = buffer + KOUTPUT_BIT_STREAM_ALLOC_SIZE;
totalBits = KOUTPUT_BIT_STREAM_ALLOC_SIZE * sizeof(int) * 8;
usedBits = 0;
mask = 0xFFFFFFFF;
nextBit = 31;
}
return self;
}
- (void) dealloc
{
free(buffer);
[super dealloc];
}
- (unsigned int) significantBits
{
return usedBits;
}
- (NSData *) data
{
int dataSize = ((usedBits % 8) == 0) ? usedBits / 8 : usedBits / 8
+ 1 ;
return [NSData dataWithBytes: buffer length: dataSize];
}
/*
NOTE: bit shifting math is a lot less squirrely if bits are indexed
1 - 32 rather than 0 - 31
-----------------------------------------------------------------------
-------------------------------------
32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14
13 12 11 10 9 8 7 6 5 4 3 2 1
-----------------------------------------------------------------------
-------------------------------------
0 0 • 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
-----------------------------------------------------------------------
-------------------------------------
1 0 0 1 1 1 0 1
*/
- (void) write:(int) inVal
bits:(int) inBits
{
int rightShift,
leftShift;
// grow the buffer if necessary
if ((usedBits + inBits) > sizeInBits)
[self growBuffer];
if (nextBit < inBits)
{
// define shifts
rightShift = inBits - nextBit,
leftShift = 32 - inBits;
// write value
*s |= inVal >> rightShift;
*++s |= inVal << leftShift;
// update bit offset
nextBit = 32 - inBits;
}
else if (nextBit > inBits)
{
// define shifts
leftShift = nextBit - inBits;
// write value
*s |= inVal << leftShift;
// update bit offset
nextBit -= inBits;
}
else
{
*s |= inVal;
s++;
nextBit = 32;
}
// increment usedBits
usedBits += inBits;
}
- (void) growBuffer:(int) inBits
{
// time to make the doughnuts
unsigned int sOffsetInInts = s - buffer,
inBitsInInts = (inBits * sizeof(int)) / 8,
// it's possible that inBitsInInts >
KOUTPUT_BIT_STREAM_ALLOC_SIZE so check for this and adjust accordingly
newSizeInInts = (inBitsInInts >
KOUTPUT_BIT_STREAM_ALLOC_SIZE) ? inBitsInInts +
KOUTPUT_BIT_STREAM_ALLOC_SIZE,
newSizeInBytes = newSizeInInts * sizeof(int),
*newBuffer = malloc(newSizeInBytes);
// zero new buffer
memset(newBuffer, 0, newSizeInBytes);
// copy data from buffer to newBuffer
memcpy(newBuffer, buffer, sizeInBytes);
// free the old buffer
free(buffer);
// update size variables
sizeInInts = newSizeInInts;
sizeInBytes = newSizeInBytes;
sizeInBits = sizeInBytes * sizeof(char);
// update pointers
s = newBuffer + sOffsetInInts;
e = newBuffer + sizeInInts;
}
@end
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden