0
    if (url_leng)
    {
        NSString *open_string;

        if      (g_system_status.language_code == 0)
            open_string = @"Open";
        else if (g_system_status.language_code == 1)
            open_string = @"Abrir";
        [open_string retain];
        [alert addButtonWithTitle : open_string];
        g_scan_result = targ_url;               
    }

Consider the above code segment. My question is about the "retain" statement. Somehow I need the retain statement to make the code work. My only explanation is when open_string goes out of scope, a release call will be made against it. And thus a retain call is needed to hold the value.

Hope somebody could confirm this ...

Also wish to ask if release statements for the strings are needed after the conditional block ?


Update : (After reading through your kind suggestions and valuable insights)

Have tried the following amendment :

    if (url_leng)
    {            
        if      (g_system_status.language_code == 0)
            [alert addButtonWithTitle : @"Open"];
        else if (g_system_status.language_code == 1)
            [alert addButtonWithTitle : @"Abrir"];
        else
            [alert addButtonWithTitle : @"Open"];
        g_scan_result = targ_url;               
    }

Everything seems to be ok now (even without the retain statement).

Stanley
  • 4,446
  • 7
  • 30
  • 48
  • 1
    First, if `language_code` is not 0 or 1, you might have an uninitialized value in the `open_string` variable (depending on LLVM version). Second, you're dealing with string constants here, which [can have interesting reference counting behavior](http://stackoverflow.com/questions/1390334/nsstring-retain-count). – Brad Larson Oct 30 '12 at 03:21
  • Thanks for the comment, do you have the same opinion as rmaddy ? That is a retain call should not be needed at all ... – Stanley Oct 30 '12 at 03:26
  • Also, if you are trying to internationalise your app you should probably look at https://developer.apple.com/internationalization/ as Apple provides a lot of helpful functionality, e.g. `NSLocalizedString` etc – mttrb Oct 30 '12 at 03:34
  • 1
    @Stanley The retain is unnecessary and unwanted. All you are using `open_string` for is as something to pass into `-addButtonWithTitle:` (which would retain the string itself as needed). Also, as the above-linked question points out, string constants are a special case where retain and release don't work as you think. In fact, if you used `-retain` as above for anything else, you'd be leaking memory at the end of that method, because you did not balance it with a `-release`. Again, you'll also want to set `open_string` to nil when you initialize it, or you could see bizarre behavior. – Brad Larson Oct 30 '12 at 03:54
  • Thanks for your valuable insight, will go through the whole thing again as suggested. – Stanley Oct 30 '12 at 04:05

2 Answers2

2

open_string is an NSString pointer pointing to string literal. String literals live on the stack. In your code where you are passing reference to string literal Copy them onto heap if you want to hold on to that data. If you own an object (i.e., called retain/copy/new/alloc on an object) you are responsible to release it, otherwise you would be leaking memory.

0x8badf00d
  • 6,391
  • 3
  • 35
  • 68
  • Thanks for the answer, do you think that the retain statement is necessary (or appropriate) ? – Stanley Oct 30 '12 at 03:31
  • What are you doing in addButtonWithTitle: method of alert object ? comment out retain statement. Call [open_string copy] in addButtonWithTitle: then release it before you return in that method. Follow the link in Brad Larson's comment to understand about string constants. – 0x8badf00d Oct 30 '12 at 03:33
  • Thanks for the comment, guess I need to take a deep look at this. – Stanley Oct 30 '12 at 03:42
  • The alert object is setup in response to a ZXing scan result. Am not sure if the problem is related to the imported ZXing code. But the QRScanner seems to be working as expected even though the code import process was a bit messy. – Stanley Oct 30 '12 at 04:43
  • Also thanks for the suggestions and info about string literals. Should get know more about these string constants as they are being used very often. – Stanley Oct 30 '12 at 04:50
1

The retain is definitely not needed in this code. If you are having issues, it is not here. Once open_string is assigned to the alert, you don't need it anymore. You must have a memory management issue elsewhere.

Perhaps the issue is if the language_code is other than 0 and 1. In this case you never properly initialize open_string. You should at least assign nil or handle this other case in some way.

Consider ARC and make your life so much easier.

rmaddy
  • 314,917
  • 42
  • 532
  • 579