3

What happens if I release a non-allocated object?

Example:

NSString *value = @"hello World!";

[value release]; 
sidyll
  • 57,726
  • 14
  • 108
  • 151
user891268
  • 1,425
  • 5
  • 20
  • 25

2 Answers2

2

Nothing, string literals are special when it comes to memory management. Check this question.

Community
  • 1
  • 1
sidyll
  • 57,726
  • 14
  • 108
  • 151
1

As sydill said, string literals are different, as they don't need to be allocated or released.

Releasing other non-allocated objects OTOH, also NSStrings that are not literals, will very likely cause an EXC_BAD_ACCESS exception.

Noah Witherspoon
  • 57,021
  • 16
  • 130
  • 131
Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94
  • so it may cause an exception. bad access exception: in run time – user891268 Aug 15 '11 at 22:31
  • Yes, it will very likely throw the ECX_BAD_ACC exception. It depends a bit on the value in your pointer, which is simply undefined. It is very likely complete garbage, but by chance, it could be the address of another object, and that might be released (too early, but silently., i.e. no exception) or it could be nil, NULL, which would result in a no-op. It all depends on what that part of the stack contained before. – Rudy Velthuis Aug 15 '11 at 22:35