Object Release Difficulty

Marjock
Unregistered
 
Post: #1
Okay, I have two objects.

Code:
    Surroundings *john;
    Surroundings *environment;


All that I do to john is Allocate and Initalise it

Code:
john = [[Surroundings alloc] init];


and release it

Code:
[john release];


I don't do anything at all to environment, bar the single line I've already shown. (I've gone through every file in my program both looking by eye and doing a find).

However, when I comment out or delete the line "Surroundings *environment;" I get an EXC_BAD_ACCESS error when trying to release john.

This just seems bizzare to me; anybody else have any ideas?

Cheers,
Mark
Quote this message in a reply
Sage
Posts: 1,199
Joined: 2004.10
Post: #2
Show us your Surroundings class's -init method.
Quote this message in a reply
Marjock
Unregistered
 
Post: #3
It's just this at the moment:

Code:
- init
{
    self = [super init];
    
    if(self == nil)
        return nil;
    
    return self;
}


I didn't think I needed any more than that for what I've done so far...

-Mark
Quote this message in a reply
Member
Posts: 196
Joined: 2003.10
Post: #4
Are you positive that your allocation code is being called? Made sure that you're not releasing more than once? Put john in an array and released the array (calling release on each contained object)?
Quote this message in a reply
Marjock
Unregistered
 
Post: #5
I'm honestly not doing anything with john that hasn't been listed above.
As far as checking that my allocation method is called, I've gone:

Code:
if(!(john = [[Surroundings alloc] init]))
NSLog(@"Oh no! john didn't alloc!");


And it didn't say what's in my NSLog statement. I was assuming that was enough - Maybe not?
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #6
This kind of thing usually shows up if you're trashing your stack somewhere. It's often an annoyingly difficult problem to debug. Try printing out the value of john after you alloc it, and again just before you release it, and see if it changes unexpectedly.
Quote this message in a reply
Marjock
Unregistered
 
Post: #7
Using

NSLog(@"%@", john);

I got the same result just after allocating and just before releasing, namely: "<Surroundings: 0x307180>"

-Mark
Quote this message in a reply
Sage
Posts: 1,199
Joined: 2004.10
Post: #8
I think ThemsAllTook is probably on the money here -- run it through the debugger with "Guard Malloc" turned on.
Quote this message in a reply
Marjock
Unregistered
 
Post: #9
Okay, cheers.

Guard Malloc pointed out an error in another function and getting rid of the [super dealloc] line in there seemed to fix things.

I can honestly say I don't understand at all but, er, at least it works?

Heh, thakns for all of your help,
Mark =)
Quote this message in a reply
Sage
Posts: 1,199
Joined: 2004.10
Post: #10
You should proably keep the [super dealloc] in your dealloc...
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #11
Were you calling dealloc from a method other than dealloc?
Quote this message in a reply
Marjock
Unregistered
 
Post: #12
I wasn't, no. Although, now that I think about it I might have been calling [super dealloc] in two different subclasses of NSObject which would presumable have about the same effect.

I'm going to reread one of the books I have with memory management in mind, so cheers for all your help, but don't worry about it any more; I'll try to either figure it all out or be able to post something more specific.

Thanks again,
Mark
Quote this message in a reply
Oldtimer
Posts: 832
Joined: 2002.09
Post: #13
A classic is to call [super dealloc] as the first line of your dealloc method. Try putting it last.
Quote this message in a reply
Marjock
Unregistered
 
Post: #14
Quote:A classic is to call [super dealloc] as the first line of your dealloc method. Try putting it last.


Aaah, thank you.

That and making it so I only have a dealloc method in one of my files seems to have fixed it. However, it seems to do some strange things (ie. sometimes work, sometimes not, without any code changes) so we'll see what happens Rasp

-Mark
Quote this message in a reply
Post Reply