1

I'm in the process of looking over some code in a large project, and I have noticed that in several of the classes, instance variables are created but no corresponding properties (@property) are created.

Is it "wrong" to create instance variables without properties? Doesn't this become a memory management issue?

I've actually never seen code like this before so I'm not sure what to think at this point.

Thanks in advance!

edcincy
  • 321
  • 2
  • 3
  • 14

4 Answers4

1

There's no reason that you have to use the Objective-C 2 style setters/getters to manage your instance variables - as long as the instance variable is released within the dealloc method (if indeed it's a alloced/inited object, etc.) then there's nothing to worry about.

Bear in mind that prior to Objective-C, such properties (and the whole @property/@synthesize syntax) simply didn't exist, so you had to create your own getters/setters if you deemed it necessary/convenient.

John Parker
  • 54,048
  • 11
  • 129
  • 129
1

Not at all. Instance variables work fine, and are subject to the same memory management rules as anything else: retain it before saving it to the instance var, and make sure you release it when you don't need it anymore (typically in the dealloc).

Some history here might be helpful:

In the beginning, there were only instance variables. "Properties" existed only in an informal way, by convention, for objects outside your class to access "public" data that the class exposed. You'd write your own -(Foo *)foo and -(void)setFoo:(Foo *)f methods for each of these. Those often were like boilerplate code, trivially returning the ivar in the first case, and doing the right retain/release/set dance in the latter.

So Objective-C 2.0 came along and gave us the ability to declare properties with the language syntax, and even generate the accessors for us-- lots of time and boilerplate code was saved.

As time went on, some people began to think about all ivars as "properties", public or private. The public ones appear in the .h file as @properties, but you can also create a private interface to your object in the .m file that declare your "private" @properties, so you can use the accessors inside your class. This might or might not be overkill, depending on your philosophy to it, but this I think has to the situation you see now, where naked ivars look suspicious.

They shouldn't. Instance variables happily exist without any of the other machinery. Just get your retain/release right (in non-GC runtimes).

As you get more advanced, see @bbum's answer to this question: Must every ivar be a property? for some more varsity things to think about around the benefits of properties around KVO and subclassing.

Community
  • 1
  • 1
Ben Zotto
  • 70,108
  • 23
  • 141
  • 204
  • Actually I noticed in one particular class, that there wasn't even a **dealloc** method in the .m file releasing the instance variables. – edcincy Apr 06 '11 at 19:35
  • @edcincy That's not necessarily an issue if the instance variables weren't alloced or were released elsewhere. – John Parker Apr 06 '11 at 19:39
1

@properties are merely shorthand -- very convenient short-hand -- for code you can write yourself, no magic about it.

It may also be that the @properties are declared in the implementation file within a class extension and there is no publicly accessible API for directly manipulating the instance variables.

bbum
  • 162,346
  • 23
  • 271
  • 359
0

Properties for instance variables aren't mandatory. In fact, prior to v2.0 of Objective-C, there was no such thing as properties -- you had to write your own accessors and mutators for instance variables (if you wanted to access them outside of the class). Properties can simplify memory management, but to be honest, memory management of ivars isn't that difficult, and it's not hard to handle yourself.

mipadi
  • 398,885
  • 90
  • 523
  • 479