Stack corrupted with @autoreleasepool (ARC, compiled with llvm 3.0, Fastest, Smallest [-Os])
Asked Answered
S

1

0

A small program which loads image and logs its size. It is compiled with ARC support, llvm 3.0. I run it on iPod 4.2 and get some funny numbers... The program is compiled in "Release" mode with "-Os" (default optimization for "Release" in xcode). This whole thing does NOT happen in Simulator. It looks to me that @autoreleasepool in combination with a loop corrupts stack... Note, that I had to isolate the problem for this post with this simple example.

--------->

int main(int argc, char *argv[])
{        
    @autoreleasepool
    {
        return UIApplicationMain(argc, argv, nil,
                                 @"AppDelegate");
    }    
}

@interface AppDelegate : UIWindow <UIApplicationDelegate>
@end

@implementation AppDelegate

-(void)loadImageAndLogValues
{
        // image from bundle 256x26
    UIImage *image = [UIImage imageNamed:@"Image.png"];

    for (int i = 0; i < 1; i++)
    {
      NSLog(@"size=%@", NSStringFromCGSize(image.size));
      NSLog(@"w=%f", image.size.width);
      NSLog(@"h=%f", image.size.height);
      NSLog(@"------------------------");
    }
}

-(BOOL)application:(UIApplication*)application
didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
    self.frame = [UIScreen mainScreen].bounds;
    self.backgroundColor = [UIColor blueColor];
    [self makeKeyAndVisible];

    [self loadImageAndLogValues];

    UIGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
        initWithTarget:self 
                action:@selector(loadImageAndLogValues)];
    [self addGestureRecognizer:tap];

    return YES;
}

@end

<------------------

And this is the output after I tap the screen once ("h" is WRONG when logged after tapping! The height of the image is 26...):

2011-10-21 01:54:48.677 Tmp[2522:307] size={256, 26}
2011-10-21 01:54:48.696 Tmp[2522:307] w=256.000000
2011-10-21 01:54:48.705 Tmp[2522:307] h=26.000000
2011-10-21 01:54:48.715 Tmp[2522:307] ------------------------
2011-10-21 01:54:50.576 Tmp[2522:307] size={256, 26}
2011-10-21 01:54:50.582 Tmp[2522:307] w=256.000000
2011-10-21 01:54:50.589 Tmp[2522:307] h=256.000000
2011-10-21 01:54:50.595 Tmp[2522:307] ------------------------

Now, I remove @autoreleasepool from main():

int main(int argc, char *argv[])
{        
        //@autoreleasepool
        //{
        return UIApplicationMain(argc, argv, nil,
                                 @"AppDelegate");
        //}    
}

Run the program and tap. Still wrong value for "h", but when calling "loadImageAndLogValues" directly from "application:didFinishLaunchingWithOptions:"...

2011-10-21 02:02:08.222 Tmp[2544:307] size={256, 26}
2011-10-21 02:02:08.240 Tmp[2544:307] w=256.000000
2011-10-21 02:02:08.250 Tmp[2544:307] h=256.000000
2011-10-21 02:02:08.259 Tmp[2544:307] ------------------------
2011-10-21 02:04:59.097 Tmp[2544:307] size={256, 26}
2011-10-21 02:04:59.103 Tmp[2544:307] w=256.000000
2011-10-21 02:04:59.109 Tmp[2544:307] h=26.000000
2011-10-21 02:04:59.115 Tmp[2544:307] ------------------------

So?... ARC + llvm 3.0 + -Os + @autoreleasepool + for(;;) + image.size.width/height is not working for me :) Please help! Thank you!

S answered 21/10, 2011 at 0:20 Comment(6)
Have you reported bug to Apple bug reporting system?Illtimed
No. But I posted on apple developer forums: devforums.apple.com/message/564415#564415 No answers so far.S
If noone known about your bug how do you expect it fixed? Simple asking on some random forums won't help, you have to fill in the problem report.Illtimed
Oki :) Reported a "bug" to Apple...S
No reaction from Apple yet... why?S
I return to this problem once in a while. I found out that logging CGImageGetWidth(image.CGImage), CGImageGetHeight(image.CGImage) instead of image.size.width, image.size.height always gives correct values... Still, I want to know what is wrong with original code...S
S
1

After (incidentally) reading LLVM vs. GCC for iOS development managed to fix the problem... ARMv6 is exactly for my iPod which was giving troubles. The "fix" is to disable thumb support for ARMv6 configuration only. Great, but weird...

enter image description here

S answered 17/11, 2011 at 20:18 Comment(1)
Yes, this seems to have the same cause and same solution as the compiler bug described here: #8391807Troostite

© 2022 - 2024 — McMap. All rights reserved.