Welcome, Guest. Please login or register.

Author Topic: Exception troubles!  (Read 13236 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline DaveP

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2116
    • Show only replies by DaveP
Re: Exception troubles!
« Reply #14 from previous page: 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 Glaucus

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 4518
    • Show only replies by Glaucus
    • http://members.shaw.ca/mveroukis/
Re: Exception troubles!
« Reply #15 on: January 23, 2003, 05:25:55 PM »
Why would you need to know the reference count?  I would assume only when more then one thread references the same instance?  Sure, if you have the code handy I wouldn't mind taking a look through it.  Thanks.

  - Mike
YOU ARE NOT IMMUNE
 

Offline DaveP

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2116
    • Show only replies by DaveP
Re: Exception troubles!
« Reply #16 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:
 

  • Guest
Re: Exception troubles!
« Reply #17 on: January 24, 2003, 09:37:49 AM »
Quote

DaveP wrote:
What C++ compilers do we have?

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

Anyone know of any more?


i once heard something of a C/C++ compiler named ColdFire. i don't know about it's current status, though.
 

Offline KarlosTopic starter

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16867
  • Country: gb
  • Thanked: 4 times
    • Show only replies by Karlos
Re: Exception troubles!
« Reply #18 on: January 24, 2003, 09:46:12 AM »
Quote

Glaucus wrote:
Why would you need to know the reference count?  I would assume only when more then one thread references the same instance?  Sure, if you have the code handy I wouldn't mind taking a look through it.  Thanks.

  - Mike


Smart pointers hare handy for all kinds of things. I wrote one as part of a cacheing implementation for managing volatile data loaded from disk in a preallocated block of memory. When the reference count hits zero, the space used by the object was up for recycling.

I also used a variant of this smart pointer that was able to write back data modifications to files once the reference count was zero (prior to releasing the memory). All interesting stuff.
int p; // A
 

Offline nicholas

Re: Exception troubles!
« Reply #19 on: December 05, 2010, 12:21:56 AM »
Quote from: DaveP;9359
What C++ compilers do we have?

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

Anyone know of any more?



How could you forget VBCC?

Bloody Heretic! ;)
“Een rezhim-i eshghalgar-i Quds bayad az sahneh-i ruzgar mahv shaved.” - Imam Ayatollah Sayyed  Ruhollah Khomeini
 

Offline KarlosTopic starter

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16867
  • Country: gb
  • Thanked: 4 times
    • Show only replies by Karlos
Re: Exception troubles!
« Reply #20 on: December 05, 2010, 12:29:51 AM »
When I posted a link to this thread, I wasn't expecting it to be resurrected...

Quote from: nicholas;597133
How could you forget VBCC?

Bloody Heretic! ;)


VBCC, though an excellent C compiler for Amiga, does not compile C++. It supports a subset of the C99 specification, however.
int p; // A
 

Offline nicholas

Re: Exception troubles!
« Reply #21 on: December 05, 2010, 12:32:04 AM »
Quote from: Karlos;597136
When I posted a link to this thread, I wasn't expecting it to be resurrected...



VBCC, though an excellent C compiler for Amiga, does not compile C++. It supports a subset of the C99 specification, however.


Well that's me looking like a right tit then! :lol:
“Een rezhim-i eshghalgar-i Quds bayad az sahneh-i ruzgar mahv shaved.” - Imam Ayatollah Sayyed  Ruhollah Khomeini
 

Offline KarlosTopic starter

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16867
  • Country: gb
  • Thanked: 4 times
    • Show only replies by Karlos
Re: Exception troubles!
« Reply #22 on: December 05, 2010, 12:33:00 AM »
@nicholas

Since you resurrected the thread, I might as well point out that the original issue went away (with several others) after migrating the code to StormC4's gcc implementation and linking with threadsafe.lib

No point in soliciting replies to a problem that was fixed 6 years ago :lol:

Quote
Well that's me looking like a right tit then!

Look before you leap!

So, since we've broadened it to C, what else (other than VBCC) do we have?

GCC, Aztec, Lattice, Maxxon, DICE...
int p; // A
 

Offline KarlosTopic starter

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16867
  • Country: gb
  • Thanked: 4 times
    • Show only replies by Karlos
Re: Exception troubles!
« Reply #23 on: December 05, 2010, 12:38:27 AM »
Quote from: DaveP;9366
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...


Seven years later and it's a feature of the OS... coincidence? :lol:
int p; // A
 

Offline Boot_WB

  • Hero Member
  • *****
  • Join Date: Feb 2005
  • Posts: 1326
    • Show only replies by Boot_WB
    • http://www.hullchimneyservices.co.uk
Re: Exception troubles!
« Reply #24 on: December 05, 2010, 02:30:34 PM »
Quote from: Karlos;597138
@nicholas

Since you resurrected the thread, I might as well point out that the original issue went away (with several others) after migrating the code to StormC4's gcc implementation and linking with threadsafe.lib

No point in soliciting replies to a problem that was fixed 6 years ago :lol:



Look before you leap!

So, since we've broadened it to C, what else (other than VBCC) do we have?

GCC, Aztec, Lattice, Maxxon, DICE...


SAS (I just found my ADFs of this recently, after considering whether or not to dispose of the manuals)
Mac Mini G4 (1.5GHz, 64MB VRam, 1GB Ram): MorphOS 3.6
Powerbook 5.8 (15", 1.67GHz, 128MB VRam, 1GB Ram): MorphOS 3.8.

Windows-free since 2011-2014 (Damn you Netflix!)
 

Offline KarlosTopic starter

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16867
  • Country: gb
  • Thanked: 4 times
    • Show only replies by Karlos
Re: Exception troubles!
« Reply #25 on: December 05, 2010, 02:58:30 PM »
Quote from: Boot_WB;597245
SAS (I just found my ADFs of this recently, after considering whether or not to dispose of the manuals)


Wow, SAS-C. How'd I manage to miss that?
int p; // A