473,804 Members | 3,182 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Beta 2: Automatic Dispose inaction?

Hello,

The following doesn't compile due to absence of the copy constructor in class FileStream:

FileInfo ^ fi = ...;
FileStream fs = fi->OpenRead();

The compiler is Beta 2. Is this supported? Planned to be supported?
Nov 17 '05 #1
11 1309
Alexei wrote:
Hello,

The following doesn't compile due to absence of the copy constructor
in class FileStream:

FileInfo ^ fi = ...;
FileStream fs = fi->OpenRead();


FileStream^ fs = fi->OpenRead();

-cd
Nov 17 '05 #2
Carl Daniel [VC++ MVP] wrote:
Alexei wrote:
Hello,

The following doesn't compile due to absence of the copy constructor
in class FileStream:

FileInfo ^ fi = ...;
FileStream fs = fi->OpenRead();


FileStream^ fs = fi->OpenRead();


Don't answer just to answer. Look here: http://blogs.msdn.com/arich/ (DF
pattern).
Nov 17 '05 #3

"Alexei Zakharov" <al********@at. gmail.com> wrote in message
news:uu******** ******@TK2MSFTN GP15.phx.gbl...
Carl Daniel [VC++ MVP] wrote:
Alexei wrote:
Hello,

The following doesn't compile due to absence of the copy constructor
in class FileStream:

FileInfo ^ fi = ...;
FileStream fs = fi->OpenRead();


FileStream^ fs = fi->OpenRead();


Don't answer just to answer. Look here: http://blogs.msdn.com/arich/ (DF
pattern).


This pattern is for C++/CLI implemented classes only, the FCL are written in
C# and their classes can't be "stack allocated". So, what you should do is,
allocate the object on the heap and manually dispose.

Willy.

Nov 17 '05 #4
"Willy Denoyette [MVP]" <wi************ *@telenet.be> wrote in message
news:uK******** ******@TK2MSFTN GP14.phx.gbl
This pattern is for C++/CLI implemented classes only, the FCL are
written in C# and their classes can't be "stack allocated". So, what
you should do is, allocate the object on the heap and manually
dispose.


It doesn't seem plausible since I can write:
FileStream fs("myfile", FileMode::Creat e);
And it will work.
Nov 17 '05 #5

"Alexei" <al*****@at.gma il.com> wrote in message
news:uI******** *****@tk2msftng p13.phx.gbl...
"Willy Denoyette [MVP]" <wi************ *@telenet.be> wrote in message
news:uK******** ******@TK2MSFTN GP14.phx.gbl
This pattern is for C++/CLI implemented classes only, the FCL are
written in C# and their classes can't be "stack allocated". So, what
you should do is, allocate the object on the heap and manually
dispose.


It doesn't seem plausible since I can write:
FileStream fs("myfile", FileMode::Creat e);
And it will work.


Sorry for not being more explicit. The FileInfo->OpenRead returns a
FileStream object reference (heap allocated), you can't assign it to a stack
allocated variable.
So what you could do is...

FileInfo fi(".....");
FileStream ^fs = fi.OpenRead();

Willy.

Nov 17 '05 #6
Willy Denoyette [MVP] wrote:
"Alexei" <al*****@at.gma il.com> wrote in message
news:uI******** *****@tk2msftng p13.phx.gbl...
"Willy Denoyette [MVP]" <wi************ *@telenet.be> wrote in message
news:uK******** ******@TK2MSFTN GP14.phx.gbl
This pattern is for C++/CLI implemented classes only, the FCL are
written in C# and their classes can't be "stack allocated". So, what
you should do is, allocate the object on the heap and manually
dispose.
It doesn't seem plausible since I can write:
FileStream fs("myfile", FileMode::Creat e);
And it will work.


Sorry for not being more explicit. The FileInfo->OpenRead returns a
FileStream object reference (heap allocated), you can't assign it to
a stack allocated variable.


There is no such thing as stack allocation for ref classes. So the whole
your argument is moot.

{
FileStream fs(...);
}

is equivalent to:

{
FileStream ^ fs = gcnew FileStream(...) ;
try {
} finally {
delete fs;
}
}

See, the object is still on GC heap. You can't place it on stack. What you
have is just a language construct that sort of looks like a stack-based
placement.

Now, does it matter if the object is created with gcnew or by calling a
function? No, IMO. The same code I can rewrite with using fi.OpenRead():

{
FileStream ^ fs = fi.OpenRead();
try {
} finally {
delete fs;
}
}

What leads me to simply:

FileStream fs(fi.OpenRead( ));

I can't understand the point of having copy-constructors for ref classes.
..NET object model is fundamentally different from C++ object model. There
is no copy-construction per se in .NET (besides trivial bitwise copy for
value classes). Objects are passed around by reference not by value as in
C++, so copying is not needed. I don't see the reason for C++/CLI to
introduce an idiom that can't have any relevence for .NET programming.
So what you could do is...

FileInfo fi(".....");
FileStream ^fs = fi.OpenRead();

Nov 17 '05 #7
> There is no such thing as stack allocation for ref classes.

The point is "stack semantics", not "stack allocation". The memory for
the object is indeed allocated from GC heap, but it is (mostly)
irrelevant here.

Nov 17 '05 #8
Who's talking about stack allocated reference classes, I said "assign it to
a stack allocated variable" right?
I know ref. classes aren't stack allocated, they have stack semantics.

For the compiler there's a difference between:
FileStream fs = fi->OpenRead();
and
FileStream fs(...);
The first is an assignment of a "stack allocated variable", with stack
semantics, to a value with "reference" semantics (of type FileStream),
returned by a (C#) function call. This is something that the current C++
compiler can't handled, why he's insisting on a copy constructor is beyond
me, ref class can't have copy constructors.
The latter is a constructor call, that upon return (note: nothing is
returned from the constructor!!!) , initializes a stack allocated variable
with stack semantics. This initialization is completely handled by the C++
compiler.

Herewith a simple sample that illustrates the issue:

// C#
public class Tester
{
public Test GetTest()
{
Test t = new Test();
return t;
}
}

public class Test
{...
}
}

// C++
Tester t;
Test tt = t.GetTest();

Willy.

"Alexei" <al*****@at.gma il.com> wrote in message
news:eo******** ********@TK2MSF TNGP14.phx.gbl. ..
Willy Denoyette [MVP] wrote:
"Alexei" <al*****@at.gma il.com> wrote in message
news:uI******** *****@tk2msftng p13.phx.gbl...
"Willy Denoyette [MVP]" <wi************ *@telenet.be> wrote in message
news:uK******** ******@TK2MSFTN GP14.phx.gbl
This pattern is for C++/CLI implemented classes only, the FCL are
written in C# and their classes can't be "stack allocated". So, what
you should do is, allocate the object on the heap and manually
dispose.

It doesn't seem plausible since I can write:
FileStream fs("myfile", FileMode::Creat e);
And it will work.


Sorry for not being more explicit. The FileInfo->OpenRead returns a
FileStream object reference (heap allocated), you can't assign it to
a stack allocated variable.


There is no such thing as stack allocation for ref classes. So the whole
your argument is moot.

{
FileStream fs(...);
}

is equivalent to:

{
FileStream ^ fs = gcnew FileStream(...) ;
try {
} finally {
delete fs;
}
}

See, the object is still on GC heap. You can't place it on stack. What
you have is just a language construct that sort of looks like a
stack-based placement.

Now, does it matter if the object is created with gcnew or by calling a
function? No, IMO. The same code I can rewrite with using fi.OpenRead():

{
FileStream ^ fs = fi.OpenRead();
try {
} finally {
delete fs;
}
}

What leads me to simply:

FileStream fs(fi.OpenRead( ));

I can't understand the point of having copy-constructors for ref classes.
.NET object model is fundamentally different from C++ object model. There
is no copy-construction per se in .NET (besides trivial bitwise copy for
value classes). Objects are passed around by reference not by value as in
C++, so copying is not needed. I don't see the reason for C++/CLI to
introduce an idiom that can't have any relevence for .NET programming.
So what you could do is...

FileInfo fi(".....");
FileStream ^fs = fi.OpenRead();


Nov 17 '05 #9
Alexei wrote:
Now, does it matter if the object is created with gcnew or by calling a
function? No, IMO. The same code I can rewrite with using fi.OpenRead():

{
FileStream ^ fs = fi.OpenRead();
try {
} finally {
delete fs;
}
}
Something like this would be nice, but it can't be done the way you're
trying to do it.
What leads me to simply:

FileStream fs(fi.OpenRead( ));
I think the code above would expand to

FileStream^ fs = gcnew FileStream(fi.O penRead());
[...]

and there is no such copy constructor in FileStream (and even if there
was one, you wouldn't want to make a copy in this case).
I can't understand the point of having copy-constructors for ref classes.


There is such a thing as copy c'tor and assignment operator in C++/CLI.
Because what if you want to make a copy? You could always define a
static member (class method) name CopyMe, but C++/CLI was designed to be
as conforming with C++ as possible. The stack syntax with ref classes
has to work like stack objects in native C++, otherwise you're inventing
a new language that has nothing to do with C++. Exactly that happened
with the old MC++, where the destructor syntax was used for the
finalizer. Such a radical change has disadvantages, because C++
programmers have certain expectations about the language, such as the
copy c'tor syntax, even if it's implemented in the underlying .NET layer
differently.

What you're asking for is

cli::auto_ptr<F ileStream> fs(fi.OpenRead( ));

which may or may not exist. If it doesn't, you can try to write your
own. It would have a little bit of an overhead, but only a couple of
extra CLI instructions.

Here's an auto pointer template that I wrote, although it has a
completely different purpose (to store unmanaged objects inside a
managed wrapper):

#pragma once

template <class T>
public ref class CliScopedPtr
{
typedef CliScopedPtr<T> this_type;

public:
CliScopedPtr()
: ptr(nullptr)
{
}

explicit CliScopedPtr(T* p)
: ptr(p)
{
}

~CliScopedPtr()
{
delete ptr;
}

T* get()
{
return ptr;
}

T& operator*()
{
return *ptr;
}

T* operator->()
{
return ptr;
}

bool assigned()
{
return ptr != nullptr;
}

void swap(CliScopedP tr% other)
{
T* tmp = other.ptr;
other.ptr = ptr;
ptr = tmp;
}

void swap(CliScopedP tr^ other)
{
T* tmp = other->ptr;
other->ptr = ptr;
ptr = tmp;
}

void reset()
{
this_type().swa p(this);
}

void reset(T* p)
{
this_type(p).sw ap(this);
}

private:
T* ptr;
CliScopedPtr(Cl iScopedPtr%); // disabled
CliScopedPtr% operator=(CliSc opedPtr%); // disabled
};
Credit goes to boost::scoped_p tr for the idea.

Example:

class Unmanaged { };

CliScopedPtr<Un managed> ptr(new Unmanaged);

Tom
Nov 17 '05 #10

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

Similar topics

1
384
by: Val3 | last post by:
Hi all. I'm using Microfoft CLR Debugger to test my c# programs. It seems works fine except for one thing. When I try to modify code message "cannot currently modify this text in the editor. It is read only" appears in the status bar at the bottom. If I use Visual C# 2005 Espress Edition Beta for debugging, everithing works fine, editing included. Any suggest? Thanks in advance VV
28
3395
by: joe | last post by:
I have a simple .NET application with two or three listViews which are filled with icons and when the user click on the proper item, they display the related images. I use "image = null ; " for all images that have been used and are going to be closed. This is how ever no way to reduce the memory consumption. I have noticed , using the task manager, that garbage collector doesn't actually do any collections unless the computer becomes low...
4
4524
by: Adriano Coser | last post by:
I'm getting the following error: error C3767: PictureSource - candidate function(s) not accessible when accessing a public member function of a managed class, either from managed or unmanaged code. The class is an owner draw control implemented into a Control Library. The access is on a DLLs that support CLR with old syntax. The code compiled OK on Beta-1.
6
5010
by: John Gabriel | last post by:
I have been obtaining the following error in the output window of the IDE: 1>mt.exe:general error c101008d:Failed to write the updated manifest to the resource of file "..\debug\test.exe". The process cannot access the file because it is being used by another process. 1>Project : error PRJ0002 : Error result 31 returned from 'C:\Program Files\Microsoft Visual Studio 8\VC\bin\mt.exe'. 1>Build log was saved at...
1
2726
by: Jürgen Kahrs | last post by:
Hello, The beta alpha release (xgawk-3.1.5-beta.20060401) of the xmlgawk project is now available at SourceForge. http://sourceforge.net/projects/xmlgawk/ http://sourceforge.net/project/showfiles.php?group_id=133165 This release is based on Arnold Robbins' regular GNU Awk 3.1.5. Problem reports are supposed to go to xmlgawk-users@lists.sourceforge.net.
2
2589
by: Randy Kraemer | last post by:
There is a free Beta test of a Python 2.4 Certification test available at Brainbench.com. They are a provider of skills-based certification exams. Go to the link below to find the test. It is free to take the test. http://www.brainbench.com/xml/bb/common/testcenter/betatests.xml
11
1990
by: Ken Fine | last post by:
I am using VS.NET 2008 and like it a lot. One of the very few things I don't like is a bug that seems to spawn literally thousands of   strings, one after the other, on design view changes. Sometimes I will end up with as many as 30,000 of them. I have to do a "Replace" which is slower than I'd like. This is slowing down my work a lot. This might be related to a commercial control I'm using, or it may just be VS.NET's indentation getting...
4
2560
by: Slickuser | last post by:
How do I automatic move the X & Y to create a ellipse function as below, DrawCircle? Or there any sample source code out there? I want it move down by user input. How can I achieve this? Thanks. Imports System Imports System.Drawing
8
9389
by: Lemune | last post by:
Hi, I'm developing window service application on C# 2005. The service is to read a new excel file on certain directory, and process it to database. The service work find on XP. But when I install the application on Windows Server 2003, when i start the service it said: "The <my serviceon Local Computer started and then stop. Some service stop automatically if they have no work to do , for example ,
0
10588
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10340
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10324
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7623
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6857
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5527
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4302
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3827
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2998
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.