Click here to Skip to main content
13,197,105 members (59,394 online)
Click here to Skip to main content
Add your own
alternative version


6 bookmarked
Posted 26 Aug 2004

Bug in managed c++

, 26 Aug 2004
Rate this:
Please Sign up or sign in to vote.
Looks like bug in managed c++ with unmanaget template class/struct


Looks like bug in Managed C++ when calling virtual member function with template argument.


<PRE lang=mc++>#include "stdafx.h" #using <mscorlib.dll> #include <tchar.h> using namespace System; template <class T> struct xroot { xroot() {} xroot(T t) {} xroot(const xroot &x) {} ~xroot() {} }; class CClass { public: virtual void func(xroot<int> x) {} }; /* void fff() { xroot<int> _a; xroot<int> _b(_a); } */ int _tmain(void) { CClass *c = new CClass(); c->func(5); return 0; }


By specification C++ the calls sequence in code


should be next:
1. Call constructor xroot<int>(int t)
2. Call c->func with just constructed argument
3. Call destructor of argument
4. Return from c->func

But in Managed C++ project the sequence looks like:
1. Call constructor xroot<int>(int t)
2. Call destructor of just constructed xroot
3. Call c->func with just constructed argument
4. Call destructor of argument (again)
5. Return from c->func

but actually there no two destructor calls on one xroot - there is second xroot - constructed by default copy constructor. Yes, we have our copy constructor, but by compiler bug it isn't instanced. Instead compiler instanced default constructor. To check this uncomment fff() function - now the copy constructor will be instanced and calls sequence will be next:
1. Call constructor xroot<int>(int t)
2. Call copy constructor xroot<int>(const xroot<int> &x) with just constructed xroot
3. Call destructor of the first xroot
4. Call c->func with just constructed argument
5. Call destructor of the second xroot
6. Return from c->func

There are very interesting things - this issue exists only if:
1. In Management C++ project (Unmanaged C++ .NET works fine)
2. In template class/struct
3. Constructor of the template calls as argument of virtual member function
4. There are no any other code to push compiler to instance copy constructor


So, actually we have two bugs in Managed C++ compiler.
The first one - there is unnecessary to create second instance of argument by calling copy constructor - by specification the first one should be send to called function.
The second - compiler doesn't instancing copy constructor, but use default one.



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralImplicit Conversion Pin
dkondrad27-Aug-04 10:31
memberdkondrad27-Aug-04 10:31 
GeneralRe: Implicit Conversion Pin
J0ker27-Aug-04 11:53
memberJ0ker27-Aug-04 11:53 
GeneralRe: Implicit Conversion Pin
AlwaysStudent10-Nov-07 18:54
memberAlwaysStudent10-Nov-07 18:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171020.1 | Last Updated 27 Aug 2004
Article Copyright 2004 by J0ker
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid