Developers.SingleGlobalObjects

From Shareaza Wiki
Revision as of 18:31, 31 December 2007 by Dirtycat (talk | contribs) (Importing page from Tikiwiki)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Single Global Objects

When Shareaza runs, it creates objects from the classes defined in the source code. It uses some classes to make a lot of objects. For instance, Shareaza makes a CNeighbour object for every computer it connects to when running. When it disconnects from a computer, it deletes the object. It keeps all the CNeighbour objects in a list, and loops through them.

Other classes aren't like this. For these classes, Shareaza just needs one object, it needs it the entire time it runs, and lots of different parts of the code need to access it. These classes create single global objects.

Looking just at the name of a class, you can't tell if it's a single global object or a multiple use object. This is too bad. They are really very different, and used very differently. It would be better if they communicated this difference right away.

Multiple use objects are created on the heap. As the code runs, it makes multiple use objects with the new operator. Here's an example from CShakeNeighbour::OnHandshakeComplete.

[code ShakeNeighbour.cpp] // Make a new Gnutella neighbour object by copying values from this ShakeNeighbour one pNeighbour = new CG1Neighbour( this ); </source>

Single global objects are created on the stack, outside of any class or method. Here's an example from the very top of Neighbours.cpp.

<source lang="c">

// Create the single global Neighbours object that holds the list of neighbour computers we are... CNeighbours Neighbours; // When Shareaza starts running, this line creates a single global inst... </source>

Here are the single global objects I've encountered in Shareaza so far.

  • CNeighbours Neighbours