Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 04-23-2010, 03:32 PM   #1 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 3
Default Bad access on properly allocated object

I'm simply allocating and initializing my object - a subclass of NSObject. The object does not exist in any form outside the scope of this code snippet. The first time a function is called on the object, it causes an EXC_BAD_ACCESS error, even though it's been properly initialized, and I've implemented a bunch of other classes in the same way.
The code snippet is below, along with the implementation file. The three lines between the alloc and bad_access error are arbitrary (initializing a text view), but for the fact that they occur between the two events. Any help would be much appreciated, I'll try to lend a hand on the forum as I wait.
Thanks

Note: It's called "RemotePairingSwitch", but it doesn't implement anything besides NSObject as of now.

Code:
// object allocated here
    RemotePairingSwitch *rpSwitch = [[RemotePairingSwitch alloc] initWithName: name isOn: on version: version];
    
    remoteNameView[remotePairToS] = [[UITextView alloc] initWithFrame: CGRectMake (100, 480 + (remotePairToS * 40), 200, 30)];
    remoteNameView[remotePairToS].text = [[NSString alloc] initWithCString: name];
    remoteNameView[remotePairToS].backgroundColor = [UIColor blackColor];

    if ( [rpSwitch isNewlyPaired]) {/*EXC_BAD_ACCESS here, at isNewlypaired*/
Code:
@implementation RemotePairingSwitch

 @synthesize remoteName;
 @synthesize remoteFile;
 @synthesize wasPaired;
 @synthesize sameVersion;
 @synthesize on;

-(BOOL) isNewlyPaired {BOOL ret = (!wasPaired && self.on); return ret;}

-(id) initWithName: (char *)name isOn: (BOOL) isOn version: (BOOL) sV {

    if (self = [super init]) {
        sameVersion = sV;
        remoteName = name;
        remoteFile = NULL;
        on = isOn;
        wasPaired = FALSE;
    }
    return self;
}

- (id) init {
    if (self = [super init]) {
        sameVersion = FALSE;
        remoteName = NULL;
        remoteFile = NULL;
        on = FALSE;
        wasPaired = FALSE;
    }
    return self;
    }

@end

Last edited by bkl1989; 04-23-2010 at 03:51 PM.
bkl1989 is offline   Reply With Quote
Old 04-23-2010, 04:54 PM   #2 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 3
Talking

Okay, here was the issue: I was reallocating that array of uitextviews like this:
Code:
//remotePairToS: top of the stack. 
//remotePairSize: size of the stack
//remotePairAlloc: amount of space to add each time stack is full

    if (remotePairToS == remotePairSize) { //if the stack is full...

        remotePairSize += remotePairAlloc;

        UITextView **tempRemoteNameView = malloc(sizeof(UITextView *) * remotePairSize);
        
        for (int i = 0; i < remotePairToS; ++i)
            tempRemoteNameView[i] = remoteNameView[i];
        
        free(remoteNameView);

        remoteNameView = tempRemoteNameView;
    }
And the remotePairAlloc, via another bug, was being set to zero. I know, I know, there are classes for this. Don't bother with a lecture about classes.
So, the stack was being reallocated successfully, but not increased in size. The top-of-stack value naturally reflected bad memory, and I was writing to it.
You might say that this has nothing to do with the initialization of the object in question. Well, it doesn't! The code doesn't interact at all. But, somehow, this was causing the problem. There's a lesson in this: use the classes that they give you as often as you can, and if you write to bad memory, the most vicious bugs will emerge, and you will be on the verge of killing yourself before you solve them.
bkl1989 is offline   Reply With Quote
Reply

Bookmarks

Tags
alloc, exc_bad_access, init

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 267
18 members and 249 guests
ADY, Alsahir, dacapo, Dani77, Desert Diva, djohnson, Duncan C, F_Bryant, Grinarn, HemiMG, jansan, linkmx, M@realobjects, macquitzon216, prchn4christ, smethorst, spiderguy84
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,882
Threads: 89,228
Posts: 380,762
Top Poster: BrianSlick (7,129)
Welcome to our newest member, jansan
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:48 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0