summaryrefslogtreecommitdiff
path: root/Examples/java
diff options
context:
space:
mode:
authorWilliam S Fulton <wsf@fultondesigns.co.uk>2004-01-20 21:28:29 +0000
committerWilliam S Fulton <wsf@fultondesigns.co.uk>2004-01-20 21:28:29 +0000
commitc2d143e4428c5683e3556d4e6a6e17b724913d2d (patch)
tree27ad41e3190a794962d1b2c7850c8a5ca6d195b3 /Examples/java
parent4039bf8075f12bfe8ed4e0d691192bf9ad59de4f (diff)
downloadswig-c2d143e4428c5683e3556d4e6a6e17b724913d2d.tar.gz
Memory management fixes and comment corrections
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@5656 626c5289-ae23-0410-ae9c-e8d60b6d4f22
Diffstat (limited to 'Examples/java')
-rw-r--r--Examples/java/extend/main.java35
1 files changed, 14 insertions, 21 deletions
diff --git a/Examples/java/extend/main.java b/Examples/java/extend/main.java
index 3924ec787..ee3a94ed0 100644
--- a/Examples/java/extend/main.java
+++ b/Examples/java/extend/main.java
@@ -10,6 +10,10 @@ class CEO extends Manager {
public String getPosition() {
return "CEO";
}
+ // Public method to stop the SWIG proxy base class from thinking it owns the underlying C++ memory.
+ public void disownMemory() {
+ swigCMemOwn = false;
+ }
}
@@ -26,9 +30,9 @@ public class main {
public static void main(String argv[])
{
- // Create an instance of our employee extension class, CEO. The calls to
- // getName() and getPosition() are standard, the call to getTitle() uses
- // the director wrappers to call CEO.getPosition. e = CEO("Alice")
+ // Create an instance of CEO, a class derived from the Java proxy of the
+ // underlying C++ class. The calls to getName() and getPosition() are standard,
+ // the call to getTitle() uses the director wrappers to call CEO.getPosition().
CEO e = new CEO("Alice");
System.out.println( e.getName() + " is a " + e.getPosition() );
@@ -41,29 +45,23 @@ public class main {
EmployeeList list = new EmployeeList();
- // EmployeeList owns its items, so we must surrender ownership of objects
- // we add. This involves first calling the __disown__ method to tell the
- // C++ director to start reference counting. We reassign the resulting
- // weakref.proxy to e so that no hard references remain. This can also be
- // done when the object is constructed, as in: e =
- // CEO("Alice").__disown__()
-
-// e = e.__disown__();
+ // EmployeeList owns its items, so we must surrender ownership of objects we add.
+ e.disownMemory();
list.addEmployee(e);
System.out.println( "----------------------" );
// Now we access the first four items in list (three are C++ objects that
// EmployeeList's constructor adds, the last is our CEO). The virtual
// methods of all these instances are treated the same. For items 0, 1, and
- // 2, both all methods resolve in C++. For item 3, our CEO, getTitle calls
+ // 2, all methods resolve in C++. For item 3, our CEO, getTitle calls
// getPosition which resolves in Java. The call to getPosition is
- // slightly different, however, from the e.getPosition() call above, since
+ // slightly different, however, because of the overidden getPosition() call, since
// now the object reference has been "laundered" by passing through
// EmployeeList as an Employee*. Previously, Java resolved the call
// immediately in CEO, but now Java thinks the object is an instance of
- // class Employee (actually EmployeePtr). So the call passes through the
+ // class Employee. So the call passes through the
// Employee proxy class and on to the C wrappers and C++ director,
- // eventually ending up back at the CEO implementation of getPosition().
+ // eventually ending up back at the Java CEO implementation of getPosition().
// The call to getTitle() for item 3 runs the C++ Employee::getTitle()
// method, which in turn calls getPosition(). This virtual method call
// passes down through the C++ director class to the Java implementation
@@ -78,12 +76,7 @@ public class main {
System.out.println( "----------------------" );
// Time to delete the EmployeeList, which will delete all the Employee*
- // items it contains. The last item is our CEO, which gets destroyed as its
- // reference count goes to zero. The Java destructor runs, and is still
- // able to call self.getName() since the underlying C++ object still
- // exists. After this destructor runs the remaining C++ destructors run as
- // usual to destroy the object.
-
+ // items it contains. The last item is our CEO, which gets destroyed as well.
list.delete();
System.out.println( "----------------------" );