Note that there are some explanatory texts on larger screens.

plurals
  1. POQuestion about pointers in Objective-C
    primarykey
    data
    text
    <p>In a <code>global.h</code> file I declare</p> <pre><code>dataManager *IdataManager; </code></pre> <p>Then later when the program initializes I allocate and initialize the variable, and use it in several different views as a means of accessing a set of data that gets downloaded when the app starts up.</p> <p>Recently I noticed when I pull up one of my views, close it, and then open it again, I get a crash when trying to access the global <code>IdataManager</code>.</p> <p>I finally came to the conclusion that it's contents were being released somehow by the view being de-initialized. I looked at the only place I was using the variable:</p> <pre><code>CCandidate *currentCandidate = [IdataManager CurrentCandidate]; </code></pre> <p>So I changed this to say:</p> <pre><code>CCandidate *currentCandidate = [[IdataManager CurrentCandidate] retain]; </code></pre> <p>I wasn't sure how the retain keyword worked, but it sounded like if the view really was releasing my data, I should specify in the creation of this pointer that it should be retained.</p> <p>This fixed the crashing, but I don't really understand why this happened. Does objective-c just always release pointers you created when you dealloc it's owner? I was under the impression I was simply storing a memory address and giving it the name currentCandidate.</p> <p>CCandidate.h:</p> <pre><code>#import &lt;Foundation/Foundation.h&gt; @interface CCandidate : NSObject { int ID; NSString* FName; NSString* MName; NSString* LName; NSString* FullName; NSString* DOB; NSString* Occupation; NSString* Employer; NSString* Phone; NSString* Fax; NSString* Email; NSString* Website; NSString* Party; NSString* TwitterUName; NSString* TwitterHashTag; NSString* Biography; NSString* BiographyLink; NSString* co; NSString* cb; NSString* uo; NSString* ub; } - (id) init; @property (nonatomic, assign) int ID; @property (nonatomic, retain) NSString* FName; @property (nonatomic, retain) NSString* MName; @property (nonatomic, retain) NSString* LName; @property (nonatomic, retain) NSString* FullName; @property (nonatomic, retain) NSString* DOB; @property (nonatomic, retain) NSString* Occupation; @property (nonatomic, retain) NSString* Employer; @property (nonatomic, retain) NSString* Phone; @property (nonatomic, retain) NSString* Fax; @property (nonatomic, retain) NSString* Email; @property (nonatomic, retain) NSString* Website; @property (nonatomic, retain) NSString* Party; @property (nonatomic, retain) NSString* TwitterUName; @property (nonatomic, retain) NSString* TwitterHashTag; @property (nonatomic, retain) NSString* Biography; @property (nonatomic, retain) NSString* BiographyLink; @property (nonatomic, retain) NSString* co; @property (nonatomic, retain) NSString* cb; @property (nonatomic, retain) NSString* uo; @property (nonatomic, retain) NSString* ub; @end </code></pre> <p>CCandidate.m:</p> <pre><code>#import "CCandidate.h" @implementation CCandidate @synthesize ID; @synthesize FName; @synthesize MName; @synthesize LName; @synthesize FullName; @synthesize DOB; @synthesize Occupation; @synthesize Employer; @synthesize Phone; @synthesize Fax; @synthesize Email; @synthesize Website; @synthesize Party; @synthesize TwitterUName; @synthesize TwitterHashTag; @synthesize Biography; @synthesize BiographyLink; @synthesize co; @synthesize cb; @synthesize uo; @synthesize ub; -(id) init { self = [super init]; ID = -1; return self; } @end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload