Click here to Skip to main content
15,895,746 members
Articles / Programming Languages / C++/CLI
Article

Bug in managed c++

Rate me:
Please Sign up or sign in to vote.
1.57/5 (6 votes)
26 Aug 2004CPOL1 min read 35.7K   6   3
Looks like bug in managed c++ with unmanaget template class/struct

Introduction

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

Example

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;

}

Comment

By specification C++ the calls sequence in code

c->func(5);

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

Conclusion

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.

 

License

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


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralImplicit Conversion Pin
dkondrad27-Aug-04 10:31
dkondrad27-Aug-04 10:31 
Greetings,

Looks like there is implicit casting of an int to xroot there. Plus you are passing the class by value so there are two constructions: 1 for the implicit cast to a temporary object (which is destroyed after passing the value to the function parameter), and 1 for the pass-by-value of said temporary object. I'm surprised the compiler didn't complain here.

Try declaring an xroot in main and passing it and see what happens. Also, I thought structs used deep copying and not copy constructors when passing by value.

Dave
GeneralRe: Implicit Conversion Pin
J0ker27-Aug-04 11:53
J0ker27-Aug-04 11:53 
GeneralRe: Implicit Conversion Pin
AlwaysStudent10-Nov-07 18:54
AlwaysStudent10-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.