Click here to Skip to main content
Click here to Skip to main content

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

Introduction

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

Example

#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)

About the Author

J0ker
Web Developer
United States United States
No Biography provided

Comments and Discussions

 
GeneralImplicit Conversion Pinmemberdkondrad27-Aug-04 10:31 
GeneralRe: Implicit Conversion PinmemberJ0ker27-Aug-04 11:53 
GeneralRe: Implicit Conversion PinmemberAlwaysStudent10-Nov-07 18:54 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140709.1 | Last Updated 27 Aug 2004
Article Copyright 2004 by J0ker
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid