473,386 Members | 1,668 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Is auto_ptr allowed in managed C++ class

Jon
Is auto_ptr allowed in managed C++ class?

I have my doubts because of the following example:

#include "stdafx.h"
#include <iostream>
#include <memory>
using namespace std;

#using <mscorlib.dll>

using namespace System;

#pragma unmanaged
int ctorDtorCount = 0;

struct S {
S() {
++ctorDtorCount;
}
~S() {
--ctorDtorCount;
if( ctorDtorCount < 0 )
throw "Error";
}
};

auto_ptr<S> produce() {
return auto_ptr<S>( new S() );
};

void consume( auto_ptr<S> s ) {
}
#pragma managed

int _tmain()
{
consume( produce() );
return 0;
}
Nov 17 '05 #1
6 1520
Hi Jon,

From my view of the code snippet, the struct S is not a mamaged(__gc)
class, and function consume is also compiled as native code for it included
inside the #pragma unmanaged block.
Thanks!

Best regards,

Gary Chang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------

Nov 17 '05 #2
Jon
Thanks for the reply.

Yes, I agree that the struct S{}, the function produce(), and the function consume() are unmanaged.

However, the function _tmain() is managed.
This _tmain() function uses these unmanaged functions as follows:
1) The function produce() constructs a S instance, wrapped in a temporary auto_ptr<S> instance.
2) The function consume() deletes this S instance which is wrapped in the temporary auto_ptr<S> instance.

My code snippet show that this S instance gets deleted twice!!!

So my question is: Is this a compiler bug, or are auto_ptr's are not allowed in managed code? .
Nov 17 '05 #3
Hi Jon,

There is a know issue (that has a knowledgebase article in the process of
being written) in that in some cases passing a C++ object through an interop
boundary by value can lead to double destruction of the object. This aspect
of the problem is fixed in the Whidbey release for types that are not self
referential (in taking the address of this or one of the members and storing
it away).

Ronald Laeremans
Visual C++ team

"Jon" <jo*@martinsound.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Is auto_ptr allowed in managed C++ class?

I have my doubts because of the following example:

#include "stdafx.h"
#include <iostream>
#include <memory>
using namespace std;

#using <mscorlib.dll>

using namespace System;

#pragma unmanaged
int ctorDtorCount = 0;

struct S {
S() {
++ctorDtorCount;
}
~S() {
--ctorDtorCount;
if( ctorDtorCount < 0 )
throw "Error";
}
};

auto_ptr<S> produce() {
return auto_ptr<S>( new S() );
};

void consume( auto_ptr<S> s ) {
}
#pragma managed

int _tmain()
{
consume( produce() );
return 0;
}

Nov 17 '05 #4
Jon
Thank for the answer.

So the bottom line is, if I want reliable code, I should never cross a interop boundary with ant C++ object that has an destructor
that can not be called twice.

Too bad, I used a lot of auto_ptrs.
Nov 17 '05 #5
This sounds a bit ominous... Is there a version of this knowledgebase
article about? We're only a few weeks from release and undocumented stuff
like this isn't that welcome at the minute.

Cheers,

Steve

"Ronald Laeremans [MSFT]" <ro*****@online.microsoft.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi Jon,

There is a know issue (that has a knowledgebase article in the process of
being written) in that in some cases passing a C++ object through an interop boundary by value can lead to double destruction of the object. This aspect of the problem is fixed in the Whidbey release for types that are not self
referential (in taking the address of this or one of the members and storing it away).

Ronald Laeremans
Visual C++ team

"Jon" <jo*@martinsound.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Is auto_ptr allowed in managed C++ class?

I have my doubts because of the following example:

#include "stdafx.h"
#include <iostream>
#include <memory>
using namespace std;

#using <mscorlib.dll>

using namespace System;

#pragma unmanaged
int ctorDtorCount = 0;

struct S {
S() {
++ctorDtorCount;
}
~S() {
--ctorDtorCount;
if( ctorDtorCount < 0 )
throw "Error";
}
};

auto_ptr<S> produce() {
return auto_ptr<S>( new S() );
};

void consume( auto_ptr<S> s ) {
}
#pragma managed

int _tmain()
{
consume( produce() );
return 0;
}


Nov 17 '05 #6
Sorry, there is no version yet that I can share. Or that is worth sharing
yet for that matter.

Bottom line: do not pass C++ objects across an interop boundary by value if
they cannot survive double destruction. In almost all cases (but of course
not in the case of auto_ptr where it defeats the purpose) passing them by
const reference is a good workaround.

Ronald

"Steve McLellan" <sjm.NOSPAM AT fixerlabs DOT com> wrote in message
news:e7**************@TK2MSFTNGP10.phx.gbl...
This sounds a bit ominous... Is there a version of this knowledgebase
article about? We're only a few weeks from release and undocumented stuff
like this isn't that welcome at the minute.

Cheers,

Steve

"Ronald Laeremans [MSFT]" <ro*****@online.microsoft.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi Jon,

There is a know issue (that has a knowledgebase article in the process of
being written) in that in some cases passing a C++ object through an

interop
boundary by value can lead to double destruction of the object. This

aspect
of the problem is fixed in the Whidbey release for types that are not
self
referential (in taking the address of this or one of the members and

storing
it away).

Ronald Laeremans
Visual C++ team

"Jon" <jo*@martinsound.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
> Is auto_ptr allowed in managed C++ class?
>
> I have my doubts because of the following example:
>
> #include "stdafx.h"
> #include <iostream>
> #include <memory>
> using namespace std;
>
> #using <mscorlib.dll>
>
> using namespace System;
>
> #pragma unmanaged
> int ctorDtorCount = 0;
>
> struct S {
> S() {
> ++ctorDtorCount;
> }
> ~S() {
> --ctorDtorCount;
> if( ctorDtorCount < 0 )
> throw "Error";
> }
> };
>
> auto_ptr<S> produce() {
> return auto_ptr<S>( new S() );
> };
>
> void consume( auto_ptr<S> s ) {
> }
> #pragma managed
>
> int _tmain()
> {
> consume( produce() );
> return 0;
> }
>
>



Nov 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Noah Roberts | last post by:
Often I have a method which accepts a pointer to some class as an argument. Usually that pointer is managed by the caller through std::auto_ptr. I would assume that you could pass the auto_ptr in...
45
by: Jamie Burns | last post by:
Hello, I realise that I just dont get this, but I cannot see the need for auto_ptr. As far as I have read, it means that if you create an object using an auto_ptr, instead of a raw pointer, then...
5
by: Bill Burris | last post by:
How do I use auto_ptr in managed C++? Here is my existing code: namespace Alta { public __gc class CMDAQ { public: CMDAQ();
39
by: Andre Siqueira | last post by:
Hello all, I have a member function like thist: Query(const std::string & id, std::auto_ptr<Modifiermodif = std::auto_ptr<Modifier>()) when a try to instantiate a Query like ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.