Mach-O Linker Error: Duplicate Symbols

Discussion in 'iOS Programming' started by moonman239, Oct 18, 2013.

  1. moonman239, Oct 18, 2013
    Last edited: Oct 18, 2013

    moonman239 macrumors 68000

    Joined:
    Mar 27, 2009
    #1
    I have three view controllers. One loads another via pushViewController when a condition is met. The latter controller loads the last controller via pushViewController when a condition is met.
    Both the last view controller and the first have a variable named thecount. Because of this, I am getting a Mach-O Linker Error. Here's an excerpt from my error log:

    Code:
    duplicate symbol _thecount in:
    ...
    ld: 1 duplicate symbol for architecture armv7
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    
    This is leading me to suspect that the view controllers that are displayed prior to the last are retained, which is something I don't want happening.

    As I write this, I'm starting to think about using segues instead. The reason I didn't do that is because I don't want animated transitions. I know I can use custom segues, which is exactly what I will do. EDIT: That did not make a difference.
     
  2. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    A linker duplicate symbol error happens while linking. It doesn't have anything to do with runtime behavior of your app. Retain has nothing to do with it.

    You didn't show us the ...
    Two view controllers that are not related to one another can have properties or ivars with the same name without this error.

    Usually a duplicate symbol error would occur if you have file scope global variables with the same name. If that's what you've done then just rename one of the variables.

    If that's not what you've done then you need to post more info. What is the ...?
     
  3. moonman239 thread starter macrumors 68000

    Joined:
    Mar 27, 2009
    #3
    I did declare both variables in their respective implementations. I wish iOS would automatically dispose of all variables declared in the implementation of a view controller when I call pushViewController.

    EDIT: Anyways, I solved the problem by changing the variable name in the last view controller.
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    How? Show us.

    I think you are confused about memory management / structures if you think something like this is a disadvantage when in fact it's an advantage. Think of pushing view controllers like a stack. Why would you want things lower in the stack destroyed when new variables are pushed onto it? What state are they going to be in when you come back after popping off the stack?
     
  5. moonman239 thread starter macrumors 68000

    Joined:
    Mar 27, 2009
    #5
    Here's the thing. The way my app is, I have no need for the app to consume RAM by storing data from other view controllers. All data the app needs to keep will be stored in a local database.
     
  6. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #6
    There is something seriously wrong if having the same name in two different classes causes a duplicate symbol error.

    If you have a .h file:

    Code:
    //header comments
    
    @interface MyVC: UIViewController
    
    {
      //instance variables go here
      int foo;
    }
    
    //properties go here
    
    @end
    
    There is a copy of each variable that belongs to each INSTANCE of the MyVC class.

    If you have variables in your .m file that are declared outside of the

    Code:
    @implementation
    
    @end 
    block, then those are global variables, and will cause the link error you describe. Global variables are bad in almost all cases. Avoid them unless you know why they are bad, and why you need to use one anyway. At this point in your learning, you should probably avoid global variables entirely.
     

Share This Page