¿Cómo se protegen los objetos Objective-C?

In my previous question, I figured out that all Objective-C objects are declared as pointers. But in C and C++, pointers can be accessed from any function, global or not, and they seem unprotected.

How are they "protected" in Objective-C ?

preguntado el 10 de marzo de 12 a las 11:03

What are you talking about? Please add an example. "Pointers can be accessed from any function" doesn't make a lot of sense to me. -

Objects aren't protegido in any language. Even in C++ with it's private y protected members, you would be able to engineer code outside the object to access these members, if you could work out their offset into the object. -

What kind of protection do you want? Guard with gun standing behind the programmer and shouting "You're not gonna acces that pointer!!!" or what... -

@H2CO3 LOL What I mean is that pointers shouldn't be accessed through other objects. How does that happen in Objective-C? -

3 Respuestas

ObjC does not police your use of pointers.

There is type checking at compile time, so if you have a pointer to an NSNumber, and use the variable that holds it to assign to an NSString, the compiler will issue a warning. However, this is easily overridden by casting the pointer, as shown below,

NSNumber *myNumberPtr = [NSNumber initWithInt:99];

NSString *myStringPtr = (NSString *) myNumberPtr;

In this case, the compiler is told to keep quiet, but accessing myStringPtr as a string would cause 'undefined results', hopefully something obvious like a crash, but possibly something more pernicious.

Similarly, I could declare,

NSString *notActuallyAString = 0x897996789; // assigned some random value

Entonces cuando notActuallyAString is accessed at runtime, it is highy likely to cause a bad access exception as the pointer is almost certainly not pointing to an NSString, and quite possibly isn't a valid memory address at all.

This makes C (and its associated languages) powerful for low-level programming (if you actually know the memory mapped address of some hardware register, you can assign them in this way, and access hardware), but brings pretty clear risks.

It gets worse, because you may have a valid pointer at some point in the execution, but the memory that the pointer references is freed off at some later point. Then if you (wrongly) access that pointer, you again may well get an exception as the memory is no longer valid for the purpose the code assumes. Writing (assigning) a via a pointer that pointers somewhere it shouldn't is a common cause of memory corruption, which can be a devil to diagnose. For this reason, it's good practice (aka defensive coding) to make sure pointers that you've finished with are assigned to nil, so if you reuse those pointers when you shouldn't, you should get a symptom that is more easy to diagnose than some random memory corruption.

You need a good understanding of pointers to program in objC, and I would recommend reading the timeless classic reference book, 'The C Programming Language' by Kernighan & Ritchie which explains the basics of pointers, you can then build your understanding on how pointers and memory allocation is used in ObjC and C++.

respondido 10 mar '12, 12:03

I see, so the "protection" here is that the pointer can only receive what it's supposed to receive :| Thanks for the reading advice. But I MUST finish at least 15 lectures of that course before May.. So I must get exactly what I want, that's why I searched on Google, couldn't find it then came here :) - ElNavigat

The only protection is the compiler warnings at build time, but this is not true memory protection. The warning is that the pointer must only be accessed for the type it references. - tijeras

A pointer, per se, does not have any kind of protection.

You should take a look to some basics of OOP; members can be of three types: public, protected or private. This is what decides if you can access the member from outside the implementation of the class itself (not considering, of course, some kind of hacking like accessing private members modifying directly the bytes of the object. You must not, however, do something like this because it's strongly against the OO philosophy: if a member is private hay una razón, and forcing the access to it will not give you any guarantee that your code will work with future versions of the library or in other machines).

In Objective-C members are protected by default. That's what give the protection you are looking for.

respondido 10 mar '12, 12:03

In Objective-C, instance variables are not exposed by default. In Objective-C 2.0, they are exposed by properties using the @property y @synthesize syntax. (Prior to 2.0, solely by explicitly written getter/setter methods.)

That notwithstanding, it is possible to access instance variables directly using the pointer operator syntax, e.g. NSString *bar = Foo->_text; incluso cuando text is not exposed as a property.

Further, you can now declare instance variables in your implementation file, to avoid having them exposed in public header files. If you are writing framework code, this offers some 'protection' against access to ivars outside of the property accessors since they are no longer visible.

respondido 10 mar '12, 12:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.