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
commit 8ffd60bec7
7 changed files with 121 additions and 120 deletions

View file

@ -1,6 +1,30 @@
Version 1.3.28 (unreleased). 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 12/17/2005: mmatus
[Python] [Python]
- Add the -aliasobj0/-noaliasobj0 options to use with - Add the -aliasobj0/-noaliasobj0 options to use with
@ -68,42 +92,6 @@ Version 1.3.28 (unreleased).
-modern -fastdispatch -dirvtable -nosafecstrings -fvirtual -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 12/08/2005: mmatus
- Add the -fastdispatch option (fastdispatch feature). This - 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 %module example
// Do not generate the default proxy constructor or destructor // Do not generate the default proxy constructor or destructor
%nodefault Butler; %nodefaultctor Butler;
%nodefaultdestructor Butler; %nodefaultdtor Butler;
// Add in pure Java code proxy constructor // Add in pure Java code proxy constructor
%typemap(javacode) Butler %{ %typemap(javacode) Butler %{
@ -6161,9 +6161,10 @@ The <tt>swigCMemOwn</tt> remains unchanged (at false), so that the proxy does no
</p> </p>
<p> <p>
Note: prior to 1.3.28, the %nodefault directive disabled the default Note: The old %nodefault directive disabled the default constructor
constructor and destructor at the same time. Now it is required to explicitly use and destructor at the same time. This is unsafe in most of the cases,
%nodefault and %nodefaultdestructor to achieve the same result. and you can use the explicit %nodefaultctor and %nodefaultdtor
directives to achieve the same result.
</p> </p>
<p> <p>

View file

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

View file

@ -270,21 +270,33 @@ void delete_List(List *l) {
</pre></div> </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> <p>
If a C++ class does not define a destructor, SWIG will automatically And as in C++, a few rules that alters the previous behavior:
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:
</p> </p>
<ul> <ul>
@ -300,7 +312,7 @@ default constructor.
</li> </li>
<li>Default constructors and implicit destructors are not created if a class <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>
<li>Default constructors and implicit destructors are not created if any base <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> </ul>
<p> <p>
SWIG should never generate a default constructor for a class in which SWIG should never generate a default constructor, copy constructor or
it is illegal to do so. In some cases, however, it could be necessary default destructor for a class in which it is illegal to do so. In
or desired to disable the default constructor. Then the some cases, however, it could be necessary (if the complete class
<tt>%nodefault</tt> directive can be used: 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> </p>
<div class="code"> <div class="code">
<pre> <pre>
%nodefault Foo; // Disable the default constructor for class Foo. %nodefaultctor Foo; // Disable the default constructor for class Foo.
class Foo { // No default constructor is generated, unless is declared 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> </pre>
</div> </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> <p>
Note that the <tt>%nodefault</tt> has no effect if the default The directive <tt>%nodefaultctor</tt> can also be applied "globally", as in:
constructor is explicitly declared, as in the following case:
</p> </p>
<div class="code"> <div class="code">
<pre> <pre>
%nodefault; // Disable creation of default constructors %nodefaultctor; // Disable creation of default constructors
class Foo { class Foo { // No default constructor is generated, unless is declared
...
};
class Bar {
public: 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> </pre>
</div> </div>
<p> <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 to disable the generation of the default or implicit destructor, if
needed. Be aware, however, that this could lead to memory leaks in the needed. Be aware, however, that this could lead to memory leaks in the
target language. Hence, it is recommended to use this directive only target language. Hence, it is recommended to use this directive only
@ -369,33 +381,35 @@ in well known cases. For example:
<div class="code"> <div class="code">
<pre> <pre>
%nodefaultdtor Foo; // Disable the implictit/default destructor for class Foo. %nodefaultdtor Foo; // Disable the implicit/default destructor for class Foo.
class Foo { // No destructor is generated, unless is declared class Foo { // No destructor is generated, unless is declared
... ...
}; };
</pre> </pre>
</div> </div>
<p> <p>
As in the <tt>%nodefault</tt> case, the <tt>%nodefaultdtor</tt> The <tt>%nocopyctor</tt> disable the automatic generation of the copy
has no effect over classes that explicitly declare a public constructor, but also has no effect over classes that explicitly
destructor: declare a public copy constructor:
</p> </p>
<div class="code"> <div class="code">
<pre> <pre>
%nodefaultdtor Foo; // Try to disable the implictit/default destructor for class Foo. %nocopytor Foo; // Disable the copy constructor for class Foo.
class Foo { class Foo {
public: 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> </pre>
</div> </div>
<p> <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>
<p> <p>
@ -416,12 +430,11 @@ those sections in the interface or using <tt>%nodefault</tt> to fix the problem.
</p> </p>
<p> <p>
<b>Compatibility Note:</b> Prior to 1.3.28, the <tt>%nodefault</tt> <b>Note:</b> The above described <tt>%nodefault</tt>
directive and the <tt>-nodefault</tt> option also disable the directive/<tt>-nodefault</tt> option, which disable both the default
generation of the implicit destructors. This produced memory leaks constructor and the the implicit destructors, could lead to memory
across the target langauges in an indiscriminated way. If your leaks across the target languages, and is highly recommended you don't
interface needs the old behavior, use the <tt>-oldnodefault</tt> use them.
option.
</p> </p>

View file

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

View file

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

View file

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