Exception-handling in Objective-C?

Exception-handling in Objective-C?

Post by m » Tue, 12 Feb 2002 06:02:28



Anyone have any info on Objective-C's exception-handling, especially
wrt avoiding memory leaks?  Tips or techniques along the lines of
C++'s RAII, perhaps?
 
 
 

Exception-handling in Objective-C?

Post by Alwy » Tue, 12 Feb 2002 10:14:16




> Anyone have any info on Objective-C's exception-handling, especially
> wrt avoiding memory leaks?  Tips or techniques along the lines of
> C++'s RAII, perhaps?

Objective-C doesn't have exceptions as part of the language. Apple's
frameworks do, though, and so, I would expect, does Gnustep.

In C++, you have the Standard Library class auto_ptr to help you make
your code exception-safe. There can be nothing like it in Objective-C,
so you are left with that old standby, common sense. In the absence of
automatic garbage collection, it's definitely a subject that needs
further study.

Alwyn

 
 
 

Exception-handling in Objective-C?

Post by mike burrel » Wed, 13 Feb 2002 00:04:44





>> Anyone have any info on Objective-C's exception-handling, especially
>> wrt avoiding memory leaks?  Tips or techniques along the lines of
>> C++'s RAII, perhaps?
> Objective-C doesn't have exceptions as part of the language. Apple's
> frameworks do, though, and so, I would expect, does Gnustep.
> In C++, you have the Standard Library class auto_ptr to help you make
> your code exception-safe. There can be nothing like it in Objective-C,
> so you are left with that old standby, common sense. In the absence of
> automatic garbage collection, it's definitely a subject that needs
> further study.

with memory pools it's not much of an issue.  you don't have to remember to
free *everything*, just the autorelease pool.

--
 /"\                                                 m i k e   b u r r e l l

  X        AGAINST HTML MAIL,
 / \      AND NEWS TOO, dammit

 
 
 

Exception-handling in Objective-C?

Post by Marcel Weih » Wed, 13 Feb 2002 02:06:51




>> In C++, you have the Standard Library class auto_ptr to help you make
>> your code exception-safe. There can be nothing like it in Objective-C,
>> so you are left with that old standby, common sense. In the absence of
>> automatic garbage collection, it's definitely a subject that needs
>> further study.
>with memory pools it's not much of an issue.  you don't have to remember to
>free *everything*, just the autorelease pool.

And you don't even typically have to do that:  autorelease pools are
linked so the next higher pool will pick it up.

Marcel

 
 
 

Exception-handling in Objective-C?

Post by Alwy » Thu, 14 Feb 2002 06:44:23




Quote:

> with memory pools it's not much of an issue.  you don't have to remember to
> free *everything*, just the autorelease pool.

Well, I don't know. Suppose you have something like:

[x retain];
[y doSomethingWith:x]; // throws an exception, which jumps
                       // over the point where x is released
                       // and leaves y in an invalid state

I agree that autorelease pools can make life easier for the programmer,
but they are not a general solution to the problem of exception safety.

I think a lot of care is needed, and there are as yet no precise rules.

Alwyn

 
 
 

Exception-handling in Objective-C?

Post by David Ste » Sun, 17 Feb 2002 02:24:31



> Anyone have any info on Objective-C's exception-handling, especially
> wrt avoiding memory leaks?  Tips or techniques along the lines of
> C++'s RAII, perhaps?

POC has language support (Blocks or Action Expressions) for catching
-error: exceptions.

See http://users.pandora.be/stes/block98.html for a discussion on this.

 
 
 

Exception-handling in Objective-C?

Post by David Ste » Sun, 17 Feb 2002 02:38:26



> I agree that autorelease pools can make life easier for the programmer,
> but they are not a general solution to the problem of exception safety.

> I think a lot of care is needed, and there are as yet no precise rules.

The fact that there are no precise rules is a definite disadvantage of
autorelease pools.

Autorelease pools cause significant memory leaks in software.

Autorelease pools cause both,

     (1) 'underleaking' memory (freeing things that shouldn't be freed,
causing bus errors and segmentation faults)

 and (2) 'overleaking' memory (not freeing memory that should be freed,
causing programs to gradually claim more and more memory without
releasing it).

Autorelease pools are truly one of the worst additions ever to
Objective-C.

Objective-C does normally not have autorelease pools.

This is something that Apple has been adding, and we should get rid of
it.

 
 
 

Exception-handling in Objective-C?

Post by Alwy » Sun, 17 Feb 2002 07:23:11




Quote:

> Autorelease pools are truly one of the worst additions ever to
> Objective-C.

We've had this discussion before. Autorelease pools can in no way be
seen as an addition to the Objective-C language. NSAutoreleasePool is a
utility class in the Foundation framework; those who choose not to use
another class library in preference to Foundation need have nothing to
do with it.

Quote:> Objective-C does normally not have autorelease pools.

This is correct if you omit the word 'normally'.

Quote:> This is something that Apple has been adding, and we should get rid of
> it.

Foundation provides a system for the allocation and deallocation of
object memory. We are agreed that it is imperfect, but there is no
consensus on what should replace it. Perhaps one day there will be
optional automatic garbage collection tailor-made for Objective-C, but
that day is yet to come.

Alwyn

 
 
 

Exception-handling in Objective-C?

Post by John C. Randolp » Sun, 17 Feb 2002 16:27:55




> > I agree that autorelease pools can make life easier for the programmer,
> > but they are not a general solution to the problem of exception safety.

> > I think a lot of care is needed, and there are as yet no precise rules.

> The fact that there are no precise rules is a definite disadvantage of
> autorelease pools.

Here we go again...

Autorelease pools have NOTHING to do with exception handling.  They are
a delayed messaging facility, nothing more.

-jcr

 
 
 

1. exception handling in Objective-C..?

Hi there,

I am new in Objective-C and now, my work requires me to "translate" some
java programs into Objective-C -- and now I get stuck with some
exception handling functions from Java (like the "try...catch...."
block).  could anyone tell me what should I use/look into for these
functions in Objective-C..!?

Thank you,
Ann Lau

--
---------------------------------------------------------------------
For to me, to live is Christ and to die is gain.
                                             ~ Phil 1:21
 ......................................................................................

Ann Lau
Systems Developer, R&D Dept.
Servicio UniTeller Inc.

voice: 201-251-8493, x3007
Fax  : 201-251-4566
Cell : 732-599-7955

2. What is the point of \write\m@ne{}\vbox{} in \clearpage?

3. Exception handling in Objective C

4. BUG: RC1 DataSet - SP Temporary Tables or TABLE variable

5. General Exception-Handling Facility and Contract Programming for C Programmers

6. How do I configure my Winroute firewall

7. About exception-handling.

8. WTB: Sun Sparc Spares

9. Exception-Handling and Design by Contract for C-only Programs

10. General Exception-Handling Facility and Design by Contract for C Programmers

11. Q: exception-handling through an interface

12. Idea for exception-handling syntax

13. Exception safety with exceptions handling