4

New to objective-C,

#import <objc/objc.h>
#import <Foundation/Foundation.h>

@interface Test:NSObject
{
  int x,y, abc;
  NSString *v1, *v2;
}
@property int x , y, abc;
-(void) print;

@end

@implementation Test
@synthesize x,y, abc;
-(void) print
{
 NSLog (@"v1 and v2 values %i, %i ", v1, v2);
}

@end

int main ( int argc, char **argv)

{
  Test *t = [[Test alloc] init];
  /* Synthesized Set Method */
  [t setX:100];
  [t setY:200];
 /* Synthesized Get Method */
  NSLog (@"Retrieving Values %i, %i ",[t x], [t y]);

 /* another Way to retrieve the throuhg KVC Model */
 NSLog (@" KVC Retrieveal  %i ", [t valueForKey:@"x"]);

}

I did not get compile time error, but run time error i got :

2012-04-11 16:25:08.470 testpgm[22237] Retrieving Values 100, 200 
2012-04-11 16:25:08.513 testpgm[22237] autorelease called without pool for object (0x8e78ca0) of class NSMethodSignature in thread <NSThread: 0x8e23a08>
2012-04-11 16:25:08.514 testpgm[22237] autorelease called without pool for object (0x8e94610) of class NSIntNumber in thread <NSThread: 0x8e23a08>
2012-04-11 16:25:08.514 testpgm[22237]  KVC Retrieveal  149505552 

Looks like it is something to do with memory issue. some one point out the issue ?

NOTE: With all your inputs, i could resolve the autorelease issue, but still

NSLog (@" KVC Retrieveal  %i ", [t valueForKey:@"x"]);

does not print the proper value but the garbage. Am i doing something wrong?

Whoami
  • 13,930
  • 19
  • 84
  • 140
  • Your revised question has nothing to do with your original question. But the answer is that the format string "%i" specifies an integer while `valueForKey:` returns an object. – Chuck Apr 11 '12 at 20:16

4 Answers4

6

The main routine does not create an autorelease pool.

Use one of these methods depending on the version and compiler you are using.

Newer or with ARC:

int main(int argc, char *argv[])
{
    @autoreleasepool {

    // your code

    }
}

or

int main(int argc, char *argv[]) {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

     // your code

    [pool drain];
}

The code has numerous other problems such as:

NSLog (@"v1 and v2 values %i, %i ", v1, v2);

which should be

NSLog (@"v1 and v2 values %@, %@ ", v1, v2);

%@ is used to print objects, %i for integers.

The line:

NSLog (@" KVC Retrieveal  %i ", [t valueForKey:@"x"]);

is interesting because valueForKey returns an object (in this case an NSNumber) so the correct statement is:

NSLog (@" KVC Retrieveal  %@ ", [t valueForKey:@"x"]);

Running the program with these corrections produces:

Retrieving Values 100, 200 
KVC Retrieveal  100 
zaph
  • 111,848
  • 21
  • 189
  • 228
6

When you are in the run loop of an application, there is a default autorelease pool created for you. However, when you are running with your own main, you need to create an autorelease pool manually at the top of your main, and drain it periodically.

NSAutoreleasePool *myPool = [[NSAutoreleasePool alloc] init];
// Your code that uses autorelease...
[myPool drain];

If you are compiling with the new LLVM compiler, use the new @autoreleasepool feature instead.

Costique
  • 23,712
  • 4
  • 76
  • 79
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

main function must have an autorelease pool.

int main(int argc, char *argv[])
{
    @autoreleasepool {

        // you code

        return ...;
    }
}

EDIT:
Regarding your second part of question. valueForKey returns id, cast it to int

NSLog (@" KVC Retrieveal  %i ", [[t valueForKey:@"x"] intValue]);
beryllium
  • 29,669
  • 15
  • 106
  • 125
0
int main ( int argc, char **argv)

{
 NSAutoreleasePool *myPool = [NSAutoreleasePool new];
  Test *t = [[Test alloc] init];
  /* Synthesized Set Method */
  [t setX:100];
  [t setY:200];
 /* Synthesized Get Method */
  NSLog (@"Retrieving Values %i, %i ",[t x], [t y]);

 /* another Way to retrieve the throuhg KVC Model */
 NSLog (@" KVC Retrieveal  %i ", [t valueForKey:@"x"]);

 [pool drain];
}

may be it will work

gauravds
  • 2,931
  • 2
  • 28
  • 45