My Blog List

Showing posts with label oop. Show all posts
Showing posts with label oop. Show all posts

Tuesday, November 16, 2010

Over the Edge of Time: Accessing Nil!

    
OR

Why you never want to complain about waiting in the airport 
while they do a
little maintenance

The Well-Behaved Version

There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin".tty /dev/ttys000
Loading program into debugger…
Program loaded.
run
[Switching to process 31280]
Portal Pilots Maintenance Flight. Scanning for UNIQUE Time Portals
portals[0] = bridge
portals[1] = creek
portals[2] = path
portals[3] = alley
Running…

Debugger stopped.
Program exited with status value:0.

But Wait!

HERE’S WHAT HAPPENS WHEN THE ARCH-ENEMY OF PORTAL PILOTS MANAGES TO GET HIRED ONTO THE DEVELOPMENT STAFF


(just tought I'd try to access nil and see where it took me!)

// If I ever want to totally gum up the works, I'll make sure to try to access past the end of the array!


Portal Pilots Maintenance Flight. Scanning for UNIQUE Time Portals
portals[0] = bridge
portals[1] = creek
portals[2] = path
portals[3] = alley
2010-11-16 12:14:54.431 encounterPortals[31239:a0f] *** __NSAutoreleaseNoPool(): Object 0x10010cce0 of class NSCFString autoreleased with no pool in place - just leaking
2010-11-16 12:14:54.433 encounterPortals[31239:a0f] *** __NSAutoreleaseNoPool(): Object 0x1001107d0 of class NSCFString autoreleased with no pool in place - just leaking
2010-11-16 12:14:54.433 encounterPortals[31239:a0f] *** __NSAutoreleaseNoPool(): Object 0x1001023b0 of class NSException autoreleased with no pool in place - just leaking
2010-11-16 12:14:54.434 encounterPortals[31239:a0f] *** __NSAutoreleaseNoPool(): Object 0x1001108e0 of class _NSCallStackArray autoreleased with no pool in place - just leaking
2010-11-16 12:14:54.434 encounterPortals[31239:a0f] *** __NSAutoreleaseNoPool(): Object 0x100110a90 of class _NSCallStackArray autoreleased with no pool in place - just leaking
2010-11-16 12:14:54.448 encounterPortals[31239:a0f] *** __NSAutoreleaseNoPool(): Object 0x100110e30 of class NSCFString autoreleased with no pool in place - just leaking
2010-11-16 12:14:54.495 encounterPortals[31239:a0f] *** __NSAutoreleaseNoPool(): Object 0x100111440 of class NSCFString autoreleased with no pool in place - just leaking
2010-11-16 12:14:54.496 encounterPortals[31239:a0f] *** __NSAutoreleaseNoPool(): Object 0x100111890 of class NSConcreteMutableData autoreleased with no pool in place - just leaking
2010-11-16 12:14:54.497 encounterPortals[31239:a0f] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFArray objectAtIndex:]: index (4) beyond bounds (4)'
*** Call stack at first throw:
(
       0   CoreFoundation                      0x00007fff884d8cc4 __exceptionPreprocess + 180
       1   libobjc.A.dylib                     0x00007fff86f250f3 objc_exception_throw + 45
       2   CoreFoundation                      0x00007fff884d8ae7 +[NSException raise:format:arguments:] + 103
       3   CoreFoundation                      0x00007fff884d8a74 +[NSException raise:format:] + 148
       4   Foundation                          0x00007fff801db084 _NSArrayRaiseBoundException + 122
       5   Foundation                          0x00007fff8013db59 -[NSCFArray objectAtIndex:] + 75
       6   encounterPortals                    0x0000000100000dfd main + 417
       7   encounterPortals                    0x0000000100000c54 start + 52
       8   ???                                 0x0000000000000001 0x0 + 1
)
terminate called after throwing an instance of 'NSException'
Program received signal:  “SIGABRT”.
sharedlibrary apply-load-rules all
kill
quit


// encounterPortals
// PortalPilots encounter a variety of Time Portal Types
// based on accesselements page 166, Holzner

#import <Foundation/Foundation.h>

// IMPORTANT STUFF
//  1) End arrays with the nil object
//  2) Use @ to differentiat OBJECTIVE C strings from C-style strings

int main (int argc, const char * argv[]) {
    NSArray *portals = [[NSArray alloc] initWithObjects:@"bridge", @"creek",@"path", @"alley", nil];
    printf("Portal Pilots Maintenance Flight. Scanning for UNIQUE Time Portals \n");
    printf("portals[0] = %s \n", [[portals objectAtIndex: 0] cString]);
     printf("portals[1] = %s \n", [[portals objectAtIndex: 1] cString]);
    printf("portals[2] = %s \n", [[portals objectAtIndex: 2] cString]);
     printf("portals[3] = %s \n", [[portals objectAtIndex: 3] cString]);
   
    // Thought it would be interesting to try to access nil
    // Wow! Leaking - What IS leaking? And all kinds of neat errors
  
    //printf("portals[4] = %s \n", [[portals objectAtIndex: 4] cString]);

    return 0;
}

Friday, October 22, 2010

Reviewing Concepts: Identify Time Portals

Las Pilitas Time Portal
I’m finding it really useful to have worked my way down to this book, which lays out one simple concept after another. It helps me to focus on one thought at a time and how oop is different than being a structured programmer. I wonder what it will be like to go back to those other two books I used during the summer, with their much more complex examples, when I’m done with this one. It sure is tempting to jump ahead in time and find out! 

One thing I’ve found challenging learning Objective-C is knowing that I’m doing things that I’ll be unlearning later on. In this example, I know that naming each class as a particular Time Portal isn’t as cleanly object oriented as I’ll be later on. I wouldn’t setup a record in a database schema like this. Or even in an old-fashioned file-management system. These are really instances of the same class.

Now, when I say ‘instances of the same class’ am I thinking right? There’s where a background using database methodology sometimes trips me up. Like when I catch myself thinking than an object is a variable or a class is a record. They aren’t.

Come back to this later and see if I had this ‘instance’ business right or not.


……..
[Session started at 2010-10-20 09:27:32 -0700.]
GNU gdb 6.3.50-20050815 (Apple version gdb-1469) (Wed May  5 04:36:56 UTC 2010)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin".tty /dev/ttys001
Loading program into debugger…
Program loaded.
run
[Switching to process 333]
Running…
Located Portal at LasPilitasSLO.
Located Portal at MillbraeBARTStation.

Debugger stopped.
Program exited with status value:0.
 ...

#import <stdio.h>
#import <Foundation/NSObject.h>

// GUI NOTE It's SHIFT Command/Apple F to do a find & replace in XCode
//       Been trying to find global find & replace for awhile now!
//       Regular appleF just highlights all the occurances in the piece of code

// Locate and Identify Time Portals for Portal Pilots Service

// Based on ideas from Page 112 of Virtual Quickstart Guide Objective-C (Holzner)
// Multiple Object Types and use of 'id'type
//
// Create objects of two different class types
//    Place them in the same variable of type id

// NOTE ORDER of *multiple* INTERFACE & METHOD sections
//        I write the interface for one class then it's methods
//      I repeat this structure for the other class
//  

@interface LasPilitasSLO:NSObject
-(void) print;
@end

//METHODS for LasPilitasSLO

@implementation LasPilitasSLO
-(void) print
{
    printf("Located Portal at LasPilitasSLO.\n");
}

@end


@interface MillbraeBARTStation:NSObject
-(void) print;
@end

//METHODS for MillbraeBARTStation
@implementation MillbraeBARTStation
-(void) print
{
    printf("Located Portal at MillbraeBARTStation.\n");
}

@end

int main (void) {
    LasPilitasSLO *c1 = [LasPilitasSLO new];
    MillbraeBARTStation *c2 = [MillbraeBARTStation new];
    // Remember
    // Here I am declaring 'container' to be of type id
    id container;
   
    container = c1;
    [container print];
   
    container = c2;
    [container print];
   
     return 0;
}