generate implicit copyctor, add -nocopyctor, and clarify the -nodefault, -nodefaultctor, -nodefautldtor options

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@8031 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Marcelo Matus 2005-12-22 06:32:49 +00:00
parent 723281a823
commit 8ffd60bec7
7 changed files with 121 additions and 120 deletions

View File

@ -1,6 +1,30 @@
Version 1.3.28 (unreleased).
===========================
12/21/2005: mmatus
- The '-nodefault' flag (pragma and features) now generates
a warning, and recommends to use the explicit
-nodefaultctor and -nodefaultdtor options.
The reason to split the 'nodefault' behavior is that, in
general, ignoring the default destructor generates memory
leaks in the target language. Hence, is to risky just to
disable the both the default constructor and destructor
at the same time.
If you need to disable the default destructor, it is
also recommended you use the directive form:
%nodefaultdtor MyVerySpecialClass;
for specific classes, and always avoid using the global
-nodefault and -nodefaultdtor options.
- SWIG now also generates the implicit copy constructor,
and that can be also disabled using the corresponding
-nocopyctor option and/or %nocopyctor directive.
12/17/2005: mmatus
[Python]
- Add the -aliasobj0/-noaliasobj0 options to use with
@ -68,42 +92,6 @@ Version 1.3.28 (unreleased).
-modern -fastdispatch -dirvtable -nosafecstrings -fvirtual
12/09/2005: mmatus
The '-nodefault' flag (pragma and features) now only
disable the default constructor, but not the default
destructor. The reason is that ignoring the default
destructor generates memory leaks, impossible to
work around, in all the languages. Hence, is to risky
just to disable the destructor when the flag is mainly
used to ignore the default constructor.
If needed, you can still disable the destructor, by using
the feature form:
%nodefaultdtor MyVerySpecialClass;
Note: if you really really need it, you can still disable
the implicit destructors at the command line using
swig -nodefaultdtor
But really, don't do that please.
Also, you can force the old 'nodefault' behavior by using
swig -oldnodefault
*** POTENTIAL INCOMPATIBILITY ***
If you use -nodefault in your interface, now some new
destructors could be generated in the interface. This
could solve some memory leaks but also produce some
conflicts.
To restore old behavior, you can use -oldnodefault.
12/08/2005: mmatus
- Add the -fastdispatch option (fastdispatch feature). This

View File

@ -6123,8 +6123,8 @@ The following interface file code should be placed before SWIG parses the above
%module example
// Do not generate the default proxy constructor or destructor
%nodefault Butler;
%nodefaultdestructor Butler;
%nodefaultctor Butler;
%nodefaultdtor Butler;
// Add in pure Java code proxy constructor
%typemap(javacode) Butler %{
@ -6161,9 +6161,10 @@ The <tt>swigCMemOwn</tt> remains unchanged (at false), so that the proxy does no
</p>
<p>
Note: prior to 1.3.28, the %nodefault directive disabled the default
constructor and destructor at the same time. Now it is required to explicitly use
%nodefault and %nodefaultdestructor to achieve the same result.
Note: The old %nodefault directive disabled the default constructor
and destructor at the same time. This is unsafe in most of the cases,
and you can use the explicit %nodefaultctor and %nodefaultdtor
directives to achieve the same result.
</p>
<p>

View File

@ -2222,13 +2222,13 @@ differently.
</p>
<p>
If you don't want SWIG to generate default constructors when processing C++
interfaces, you can use the <tt>%nodefault</tt> directive or the
<tt>-nodefault</tt> command line option. For example:
If you don't want SWIG to generate default constructors for your
interfaces, you can use the <tt>%nodefaultctor</tt> directive or the
<tt>-nodefaultctor</tt> command line option. For example:
</p>
<div class="shell"><pre>
swig -nodefault example.i
swig -nodefaultctor example.i
</pre></div>
<p>
@ -2238,19 +2238,19 @@ or
<div class="code"><pre>
%module foo
...
%nodefault; // Don't create default constructors
%nodefaultctor; // Don't create default constructors
... declarations ...
%makedefault; // Reenable default constructors
%clearnodefaultctor; // Re-enable default constructors
</pre></div>
<p>
If you need more precise control, <tt>%nodefault</tt> can selectively target individual structure
If you need more precise control, <tt>%nodefaultctor</tt> can selectively target individual structure
definitions. For example:
</p>
<div class="code">
<pre>
%nodefault Foo; // No default constructor for Foo
%nodefaultctor Foo; // No default constructor for Foo
...
struct Foo { // No default constructor generated.
};
@ -2265,12 +2265,12 @@ struct Bar { // Default constructor generated.
Since ignoring the implicit or default destructors most of the times
produce memory leaks, SWIG will always try to generate them. If
needed, however, you can selectively disable the generation of the
default/implicit destructor by using <tt>%nodefaultdestructor </tt>
default/implicit destructor by using <tt>%nodefaultdtor </tt>
</p>
<div class="code">
<pre>
%nodefaultdestructor Foo; // No default/implicit destructor for Foo
%nodefaultdtor Foo; // No default/implicit destructor for Foo
...
struct Foo { // No default destructor is generated.
};
@ -2290,13 +2290,10 @@ has now been enabled as the default behavior.
</p>
<p>
<b>Compatibility note:</b> Prior to SWIG-1.3.28, the
<tt>-nodefault</tt> option and <tt>%nodefault</tt> directive also
disable the default or implicit destructor generation. This had the
major side effect of generating memory leaks, and is now disabled. If
your interface needs the old behavior, use the
<tt>-oldnodefault</tt> option.
<b>Note:</b> There are also the <tt>-nodefault</tt> option and
<tt>%nodefault</tt> directive, which disable both the default or
implicit destructor generation. This could lead to memory leaks across
the target languages, and is highly recommended you don't use them.
</p>

View File

@ -270,21 +270,33 @@ void delete_List(List *l) {
</pre></div>
<H3><a name="SWIGPlus_nn8"></a>6.5.2 Default constructors and implicit destructors</H3>
<H3><a name="SWIGPlus_nn8"></a>6.5.2 Default constructors, copy constructors and implicit destructors</H3>
Following the C++ rules for implicit constructor and destructors, SWIG
will try to automatically generate them even when they are not
explicitly declared in the class interface.
In general then:
<ul>
<li>
If a C++ class does not declare any explicit constructor, SWIG will
automatically generate one.
</li>
<li>
If a C++ class does not declare a explicit copy constructor, SWIG will
automatically generate one.
</li>
<li>
If a C++ class does not declare an explicit destructor, SWIG will
automatically create one.
</li>
</ul>
<p>
If a C++ class does not define a destructor, SWIG will automatically
create one following the implicit destructor rule from C++.
</p>
<p>
If a C++ class does not define any public constructors, SWIG will
automatically create a default constructor.
</p>
<p>
There are, however, a few rules that alters the previous behavior:
And as in C++, a few rules that alters the previous behavior:
</p>
<ul>
@ -300,7 +312,7 @@ default constructor.
</li>
<li>Default constructors and implicit destructors are not created if a class
defines constructors or destructors in a <tt>private</tt> or <tt>protected</tt> section.
defines them in a <tt>private</tt> or <tt>protected</tt> section.
</li>
<li>Default constructors and implicit destructors are not created if any base
@ -309,58 +321,58 @@ class defines a non-public default constructor or destructor.
</ul>
<p>
SWIG should never generate a default constructor for a class in which
it is illegal to do so. In some cases, however, it could be necessary
or desired to disable the default constructor. Then the
<tt>%nodefault</tt> directive can be used:
SWIG should never generate a default constructor, copy constructor or
default destructor for a class in which it is illegal to do so. In
some cases, however, it could be necessary (if the complete class
declaration is not visible from SWIG, and one of the above rules is
violated) or desired (to reduce the size of the final interface) to
disable the implicit constructor/desctructor generation manually.
</p>
<p>
To do so, the <tt>%nodefaultctor</tt>, <tt>%nodefaultdtor</tt> and
<tt>%nocopyctor</tt>directives can be used. Note that these directives
only affects the implicit generation, and they have no effect if
the default/copy constructors or destructor are explicitly declared in
the class interface.
</p>
<p>
For example:
</p>
<div class="code">
<pre>
%nodefault Foo; // Disable the default constructor for class Foo.
class Foo { // No default constructor is generated, unless is declared
%nodefaultctor Foo; // Disable the default constructor for class Foo.
class Foo { // No default constructor is generated, unless is declared
...
};
class Bar { // A default constructor is generated, if possible
class Bar { // A default constructor is generated, if possible
...
};
</pre>
</div>
<p>
The directive <tt>%nodefault</tt> can also be applied "globally", as in:
</p>
<div class="code">
<pre>
%nodefault; // Disable creation of default constructors
class Foo { // No default constructor is generated, unless is declared
...
};
class Bar { // No default constructor is generated, unless is declared
...
};
%makedefault; // Enable the creation of default constructors again
</pre>
</div>
<p>
Note that the <tt>%nodefault</tt> has no effect if the default
constructor is explicitly declared, as in the following case:
The directive <tt>%nodefaultctor</tt> can also be applied "globally", as in:
</p>
<div class="code">
<pre>
%nodefault; // Disable creation of default constructors
class Foo {
%nodefaultctor; // Disable creation of default constructors
class Foo { // No default constructor is generated, unless is declared
...
};
class Bar {
public:
Foo(); // The default constructor is generated, since is declared
Bar(); // The default constructor is generated, since is declared
};
%makedefault; // Enable the creation of default constructors again
%defaultctor; // Enable the creation of default constructors again
</pre>
</div>
<p>
The correspondig <tt>%nodefaultdtor</tt> directive can be used
The corresponding <tt>%nodefaultdtor</tt> directive can be used
to disable the generation of the default or implicit destructor, if
needed. Be aware, however, that this could lead to memory leaks in the
target language. Hence, it is recommended to use this directive only
@ -369,33 +381,35 @@ in well known cases. For example:
<div class="code">
<pre>
%nodefaultdtor Foo; // Disable the implictit/default destructor for class Foo.
class Foo { // No destructor is generated, unless is declared
%nodefaultdtor Foo; // Disable the implicit/default destructor for class Foo.
class Foo { // No destructor is generated, unless is declared
...
};
</pre>
</div>
<p>
As in the <tt>%nodefault</tt> case, the <tt>%nodefaultdtor</tt>
has no effect over classes that explicitly declare a public
destructor:
The <tt>%nocopyctor</tt> disable the automatic generation of the copy
constructor, but also has no effect over classes that explicitly
declare a public copy constructor:
</p>
<div class="code">
<pre>
%nodefaultdtor Foo; // Try to disable the implictit/default destructor for class Foo.
%nocopytor Foo; // Disable the copy constructor for class Foo.
class Foo {
public:
~Foo(); // The destructor is generated, since is declared
Foo();
};
%nocopytor Bar; // Has no effect on Bar
class Bar {
public:
Bar(const Bar& b); // Copy constructor is generated
};
</pre>
</div>
<p>
If you still need to prevent the generation of the default constructor
or destructor, even when they are declared in the class, you need to
use the <tt>%ignore</tt> directive.
</p>
<p>
@ -416,12 +430,11 @@ those sections in the interface or using <tt>%nodefault</tt> to fix the problem.
</p>
<p>
<b>Compatibility Note:</b> Prior to 1.3.28, the <tt>%nodefault</tt>
directive and the <tt>-nodefault</tt> option also disable the
generation of the implicit destructors. This produced memory leaks
across the target langauges in an indiscriminated way. If your
interface needs the old behavior, use the <tt>-oldnodefault</tt>
option.
<b>Note:</b> The above described <tt>%nodefault</tt>
directive/<tt>-nodefault</tt> option, which disable both the default
constructor and the the implicit destructors, could lead to memory
leaks across the target languages, and is highly recommended you don't
use them.
</p>

View File

@ -90,6 +90,7 @@ CPP_TEST_CASES += \
const_const_2 \
constant_pointers \
constover \
constructor_copy \
constructor_exception \
constructor_explicit \
constructor_value \

View File

@ -5,7 +5,7 @@
%module cplusplus_throw
%nodefault;
%nodefaultctor;
%inline %{

View File

@ -5,9 +5,10 @@
%module static_array_member
%nodefault;
%inline %{
class RB {
static char *prberror[];
public:
static char *rberror[];
};
%}