Re: Memory problem



In article <xcommerx.1vblkv@xxxxxxxxxxxxxxxxxxx>,
xcommerx <xcommerx.1vblkv@xxxxxxxxxxxxxxxxxxx> wrote:

> I'm developing a tool that does some evolutionary stuff (LCS). My basic
> types are bitstrings created like this:
>
> Code:
> --------------------
> -(id) initWithSize: (int) s
> {
> self=[super init];
> if (self){
> if (state){
> free(state);
> }

// First comment - assuming "state" is an instance variable, I
// can't imagine how the above 3 lines could be useful.

> size=s;
> state=malloc(size*sizeof(unsigned short));
> int iter;
> for(iter=0; iter < size; iter++)
> state[iter]=0;

// Second comment - the above 4 lines could be a single
// invocation of calloc().
// Third comment - what happens if your allocation fails?

> }
> return self;
> }
> --------------------
>
>
> and the release code looks like this:
>
> Code:
> --------------------
>
> -(void) dealloc
> {
> if (state){
> free(state);
> state=nil;
> }
> [super dealloc];
> }
>
> --------------------
>
>
> It's direct superclass is NSObject.
> The problem is that these bitstrings don't release their memory, when I
> run this little program, the memory is only released after the program
> terminates instead of after the removeAllObjects call.

Fourth comment - removeAllObjects doesn't release the contents of the
array. It autoreleases them. So they don't actually go away until the
pool is released.

G

--
Goal 2005: Convincing James Hetfield to cover the Strawberry Shortcake
"Are You Berry Berry Happy?" song.
.