Welcome, Guest. Please login or register.

Author Topic: Exception troubles!  (Read 26852 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline DaveP

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2116
    • Show all replies
Re: Exception troubles!
« on: January 22, 2003, 07:50:07 AM »
Well a simple test I did with GCC last night worked
fine.

When you throw an exception a stack unwind occurs
rapidly.

The problem might be the way StormC handles the
stack unwind when two tasks are present in the same context.  If I recall correctly task creation
requires part of the stack passed as a pointer but
I need to go look at docs.

My current working theory is that StormC does not
differentiate between tasks during a stack unwind.

This is Blade's area and I haven't seen
him on here for a bit since he was sent death threats.

Anyway... still working on this but I thought I would
make sure you knew I was still looking.

Have you tried this without using the dos process implementation - say use an exec task instead? That would be much easier to debug.
Hate figure. :lol:
 

Offline DaveP

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2116
    • Show all replies
Re: Exception troubles!
« Reply #1 on: January 23, 2003, 09:45:41 AM »
Well I have long since given StormC 3 up as a pile of
turd WRT its C++ support. I think v4 is based on GCC ;-)

I know what you mean about IDEs but I prefer HiSoft C++'s IDE
which shares the same compiler code origins with StormC3
AFAIR.

Anyway reproduced your problem with StormC exceptions
last night with both Dos processes and exec tasks.

When you run it through the debugger you can reproduce it
every time if the exception is thrown immediately prior to
a context swap to the child task.

So what I did was play around with the priority of the
subtask to reduce the probability that Exec would decide
to swap to the child.

Then the exception was caught in the parent... sometimes!

I actually prefer to do my development using Visual SlickEdit on
my Mandrake 9 box
exporting its display to AmiWinX and using an NFS mounted
shared filesytem to store the files. I then use Visual SlickEdit
macros to drive the GCC compiler on the Amiga by use of
rsh.

I am not a GoldEd fan but apparently that is quite good too.

I havent yet tried your other bugs out but rest assured that
erratic template support was my biggest gripe with StormC3 and HiSoftC++
but also more importantly in MSVC5!

Hate figure. :lol:
 

Offline DaveP

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2116
    • Show all replies
Re: Exception troubles!
« Reply #2 on: January 23, 2003, 10:43:10 AM »
What C++ compilers do we have?

Maxxon C++
Hisoft C++
StormC v3, v4
GCC

Anyone know of any more?
Hate figure. :lol:
 

Offline DaveP

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2116
    • Show all replies
Re: Exception troubles!
« Reply #3 on: January 23, 2003, 11:30:54 AM »
@Karlos

Unless I find anything useful to you in the next day or so
this is probably going to be my last comment on this.

C++ exception handling is one of the worst culprits for semantic
problems during porting between operating systems.

Although I like what exceptions do as far as code clarity
is concerned a couple of years ago  I took the decision
to return to passing back return codes from methods.

Since then I have experienced only the usual suspects in
getting my applications ported between platforms ( endianness,
codepages etc ).

The other problem that comes out ( aside from compiler template
support I guess ) is multiple inheritance. This seems to be implemented
erratically in compilers.

So I have returned to non virtual single inheritance and where a
seperate inheritance relationship is required make the superclass
pure virtual with no implemented constructor/destructor forcing
the subclass implementor to treat it like an "Interface" only.

Oh and always write your own smart pointer or use someone
elses. Saves a lot of work later on!
Hate figure. :lol:
 

Offline DaveP

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2116
    • Show all replies
Re: Exception troubles!
« Reply #4 on: January 23, 2003, 12:24:21 PM »
Have you found a good way of handling loading modules
by ordinal name with the Amiga yet?

I have never been able to get a convincing DLL or Shared Object
style mechanism working...
Hate figure. :lol:
 

Offline DaveP

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2116
    • Show all replies
Re: Exception troubles!
« Reply #5 on: January 23, 2003, 05:22:39 PM »
Maintains a reference count to an object and when
the reference count hits zero the object is deleted.

I can post a code example of a really good smart pointer / auto pointer if you'd like to see how it is done.
Hate figure. :lol:
 

Offline DaveP

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2116
    • Show all replies
Re: Exception troubles!
« Reply #6 on: January 24, 2003, 08:02:07 AM »
Not really. \When you pass an object by reference
the reference count increases to include the scope
of the other object, and when you return from the by reference call it decreases.

More than one object might hold a reference to the
same object instance, e.g.

BankAccount class instance 012454 might reference Person class instance DaveP and so might BankAccount class instance 012455 ( I own both those accounts ).

Thus if someone deletes BankAccount 012454 only the reference count to DaveP is decremented. If they delete the 012455 then the reference count is zero and DaveP gets dropped from memory. That is of course if it is not held by another container class that looks after Person instances.

In fact when you start looking at the difference between active ( in memory ) instances vs passivated ( in storage and no longer in memory ) it gives you a neat lead.

Further to this if there are multiple places where the object could need to be removed and it is run time conditional.

 say you are in a complex nesting of try/catch blocks and the object instance is allocated somewhere in the mid point.

You could follow the laborious principle that each level of nesting that makes an allocation that the parent nesting is aware of should catch every exception de-allocate and rethrow. However that leads to ridiculously inpenetrable code.

Better to rely on the stack unwind principle. Thus a smart pointer allocated in the mid point would automatically deallocate the memory ( if it is not used elsewhere - an important point when you start getting into using container classes ) during unwind and therefore you do not need to catch rethrow just for deallocation or somehow "inform the parent" of the allocation or WORSE leak because you weren't aware of what will happen during an exception throw.

When might this be important? Well say the midpoint in the try catch not only creates a new bank account but adds it to the container for all bank accounts. This means the reference count is now two. If the bank account is passed to a child scope then the reference count is now three. IF an exception is thrown the reference count is now one and the object does not get deleted. Fine if you want to up front determine what the code is doing you could probably set some flag to indicate that the bank account memory is now owned not by the original scope of the allocator - but that is the point. In complex situations ( which occur 8 times out of 10 ) you start setting flags in order to give a catch block or some other code some indication of how to handle allocations and de-allocations depending on the flow of logic.

Better, more elegant and even easier for the human mind to manage ( and for subsequent program maintainers such as change teams ) if the decision to use auto/smart pointers and reference counting up front.

Note that to implement a good auto pointer ( not the one found by default in later revisions of C++ ) you are best putting the reference counting in some parent "Object" class AND the auto pointer has to be passed "by value" everywhere to force the copy constructor to do its work.

Hate figure. :lol: