|
In C++/CLI, the dispose pattern is:
ref class Base
{
public:
Base(void)
{
}
~Base(void)
{
// Release managed resources
this->!Base();
}
protected:
!Base(void)
{
// Release unmanaged resources
}
};
So, the methods Dispose() and Dispose(bool disposing) aren't available at compile-time. Also, I believe you cannot override a virtual dtor!
-- modified at 21:45 Saturday 20th May, 2006
|
|
|
|
|
If you're doing somthing like the following in C#:
public class test
{
protected override void Dispose(bool disposing)
{
if (disposing)
{
// do A
}
// do B
}
}
Then the C++/CLI equivalent is this:
public ref class test
{
public:
~test()
{
DisposeObject(true);
}
private protected:
!test()
{
DisposeObject(false);
}
private:
void DisposeObject(bool disposing)
{
if (disposing)
{
// do A
}
// do B
}
};
Our Instant C++ C# to C++ converter maintains the original 'Dispose' method, but renames it and makes it private - called only from the new CLI destructor and finalizer. This is to make conversion easier to understand, but you could dispense with the Dispose altogether and code that logic within the destructor and finalizer directly.
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter and VB to C++ converter
Instant J#: VB to J# converter
Clear VB: Cleans up VB.NET code
Clear C#: Cleans up C# code
|
|
|
|
|
Thanks for the answers, but it doesn't exactly solve my problem:
If you take the 'test' class you specified and compile it as a C# lib .dll, and then inherit it in the C++/CLI project, how do you override the original 'test::Dispose(bool disposing)'?
It looks like there's no way to do it from C++/CLI? From the C#, you just override it.
I guess that in pure C++ something like that would not be possible at all - overriding the virtual method called from parent's destructor will still cause call to the original parent's method (or maybe compile-time error or something like that, depending on the complier)?
But I guessed that since it's possible in C#, it should be possible in C++/CLI, am I wrong somewhere?
For example, if you want to force BinaryWriter/Reader not to close the underlying stream when it's disposed, how would you do it from C++/CLI?
|
|
|
|
|
Filip Strugar wrote: But I guessed that since it's possible in C#, it should be possible in C++/CLI, am I wrong somewhere?
C# and C++/CLI are distinct languages. You can write whole ASP.NET applications with C# but not with C++/CLI. You can embedded unmanaged code in C++/CLI but not with C#.
I don't think you can override the dispose pattern in C++/CLI due to the employment of deterministic finalization in managed code.
|
|
|
|
|
Yes - after doing some research on this, it appears that it's not even possible to override the original Dispose(bool) method for a base class, due to 'Dispose' being reserved.
It's strange that this is not allowed. Even a C# class derived from a C++/CLI base class will allow overriding the implicit Dispose(bool) method generated internally by the C++/CLI class having a destructor (since a C++/CLI class will implicitly implement the IDisposable interface when containing a destructor).
If anyone knows of a way to do this (or a reason it cannot be allowed), I'd be very interested.
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter and VB to C++ converter
Instant J#: VB to J# converter
Clear VB: Cleans up VB.NET code
Clear C#: Cleans up C# code
|
|
|
|
|
I have several static libraries in native c++. I am working on a new project that will use these libraries. Since this is a new project I was thinking of using C++/CLI for creating GUI using WinForms and use functionality from native static libraries. So I created a new Windows Form Application and declared an instance of a class defined [from one of the library] in the Form1.h file. It builds fine but gives a runtime exception. Then I compiled that library with /clr switch and now application runs fine.
So my question is if I want to use native static libraries from c++/cli, should I recompile all libraries with /clr switch? Or am I missing something very obvious.
thanks
-Saurabh
|
|
|
|
|
What is the runtime exception? Who is throwing it?
You should not need to rebuild all your static libraries with /clr. You only need the /clr switch for the interop files - those the bridge the native and the managed world.
gmileka
|
|
|
|
|
While I'm posting possible problems I've found...
When a struct (or class for that matter) with an Int32 ^ is deserialized an exception is thrown.
Again: Is this a "feature" that I just haven't seen documented anywhere, or an actual issue?
----CODE----
using namespace System;
using namespace System::IO;
using namespace System::Runtime::Serialization::Formatters::Binary;
[Serializable]
public ref struct TestStruct {
Int32^ i;
};
[Serializable]
public ref struct TestStruct2 {
int i;
};
int main(array<System::String ^> ^args)
{
//MemoryStream to do use for serializing
MemoryStream^ ms = gcnew MemoryStream();
//Binary Format
BinaryFormatter^ bf = gcnew BinaryFormatter();
//Create the structure
TestStruct ^t = gcnew TestStruct();
//Initialize the data
t->i = 42;
//Serialize the string
bf->Serialize(ms,t);
//GO back to the beginning of the stream to deserialize
ms->Seek(0,SeekOrigin::Begin);
try {
Object^ o = bf->Deserialize(ms);
System::Console::WriteLine("o is {0}",o);
} catch(System::Runtime::Serialization::SerializationException^ e) {
System::Console::WriteLine("Caught an exception while trying to deserialize TestStruct!");
System::Console::WriteLine(e->Message);
}
/* Generates Exception:
Type: System.Runtime.Serialization.SerializationException
Additional information: Binary stream '42' does not contain a valid
BinaryHeader. Possible causes are invalid stream or object version
change between serialization and deserialization.
Note the 42. If t->i is set to a different value, this changes.
*/
//Reset for second test
ms = gcnew MemoryStream();
bf = gcnew BinaryFormatter();
TestStruct2 ^t2 = gcnew TestStruct2();
t2->i = 42;
bf->Serialize(ms,t2);
ms->Seek(0,SeekOrigin::Begin);
try {
Object^ o = bf->Deserialize(ms);
System::Console::WriteLine("o is {0}",o);
} catch(System::Runtime::Serialization::SerializationException^ e) {
System::Console::WriteLine("Caught an exception while trying to deserialize TestStruct2!");
System::Console::WriteLine(e->Message);
}
/* No Exception Generated */
return 0;
}
----CODE END----
|
|
|
|
|
I think you need to change the defintion of TestStruct i from
Int32^ i;
to
Int32 i;
for this to work.
gmileka
|
|
|
|
|
Yes, while I agree that that does work, what I really want to know is WHY cant I use Int32^ when things like String^ work just fine.
I think there should be some documentation somehwere that says "You can't serialize certain references.. <insert list of "bad" references>" somewhere.
Myabe there already is, but i can't find it.
|
|
|
|
|
Code is self explanitory. Adding braces around the for each block fixes it, but this shouldn't be required. Unless of course you can point me to something that says this is expected behaviour.
Jody Steele
-----CODE START-----
using namespace System;
using namespace System::Collections;
int main(array<System::String ^> ^args)
{
//Define a test array
ArrayList test;
//Populate it with some data
test.Add(3);
test.Add(4);
test.Add(13);
test.Add(42);
//Works as expected: WriteLine not called
if (false)
Console::WriteLine("Test");
//Works as expected: for not entered
if (false)
for(int i=0;i<test.Count;i++) {
Console::Write("for ");
Console::WriteLine(test[i]);
}
//UNEXPECTED: for each loop is entered!
if (false)
for each (int i in test) {
Console::Write("for each ");
Console::WriteLine(i);
}
}
-----CODE END-----
|
|
|
|
|
Just in case my message about what fixes it wasn't clear:
Bad:
----CODE----
if (false)
for each (int i in test) {
Console::Write("for each ");
Console::WriteLine(i);
}
----CODE END----
Good:
----CODE----
if (false) {
for each (int i in test) {
Console::Write("for each ");
Console::WriteLine(i);
}
}
----CODE END----
But yeah... Original question still stands: Is this a "feature" or a genuine bug?
|
|
|
|
|
Definitely looks like a bug to me.
Regards,
Nish
|
|
|
|
|
Hi Friends,
Is it possible to write a parser which will find all the variable names from any program( or language specific)...and then can we change the variable name in all the files by changing variable name at just only one place
Renjith
|
|
|
|
|
IS the "Replace in files" commands in VC not sufficient for that ? Do you want to do it programmatically ?
Cédric Moonen
Software developer
Charting control
|
|
|
|
|
All I need to do is to send a structure via SerialPort Control which needs in return an array of Bytes, and then convert it back on the other side!
Can anyone help me here
I am getting pretty desperate here
Thanks
|
|
|
|
|
Hello,
you might want to give the BinaryFormatter class a try. It allows you to serialize and deserialize objects to and from a stream. As far as i know you could then retrieve the buffer from it or even write the stream to the serial port.
You will find it in this namespace:
System::Runtime::Serialization::Formatters::Binary;
Anyway, I cannot guarantee that it will actually work, because i haven't had the time yet to test it by my own.
Best regards Tobias
|
|
|
|
|
Hi all around there!
I´ve some Problems whith comiling a managed c++ programm written in vs2003 under vs2005 whith compiler-otion /clr:oldSyntax.
error c3767 occures at line: >CmbChnl1->Events->Dispose(); or CmbChnl1->get_Events()->Dispose();
and many other lines with the Dispose()-Function.
I found out something in the msdn, but it helps not very much.
It tells me that my system-own types are in .Net V2 private now.
Sorry, it's my first SW written in .Net and it's not written by myself.
Thank's a lot for helping!
|
|
|
|
|
C++/CLI does not allow you to call Dispose() anymore. You have to use delete to dispose. For example:
delete CmbChnl1->Events;
|
|
|
|
|
That doesnt fix the Problem, i got the same error-code now, (c3767: Function is not accessable) and after that a fatal error.
I try to disable all events of a form while actualizing the Data in Comboboxes.
construction:
public __gc class TransFreqView : public ModulView //public System::Windows::Forms::Form
{
public: TransFreqView()
{
GenerateComboArray();
InitializeComponent();
GenerateComboBoxes();
__mcTemp__3 = new ArrayList();
}
.....
public: void GenerateComboBoxes(void)
{
CmbChnl1->Items->AddRange(__mcTemp__1);
.....
public: System::Windows::Forms::ComboBox * CmbChnl1;
.....
public: System::Void CmbChnl1_SelectionChangeCommitted(System::Object * sender, System::EventArgs * e);
...
cpp-File:
void TransFreqView::UpdateView(void)
{
CmbChnlSetAll->Events->Dispose(); // old: CmbChnl1->get_Events()->Dispose();
....
Thanks a lot for help!
|
|
|
|
|
So, what is this CmbChnlSetAll->Events property is disposing! Also, if the Dispose() function is not accessible it is probably has protected or private access, and you are not required to manually dispose of its resources.
|
|
|
|
|
George L. Jackson wrote: C++/CLI does not allow you to call Dispose() anymore.
He is using /clr:oldSyntax option, so it should not be an issue.
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
|
|
|
|
|
I believe your problem might be on how you set up your class to process disposal of resources. Make sure your class looks something like the following:
__gc class Test : public IDisposable
{
public:
Test() : disposed(false)
{
Console::WriteLine(__FUNCSIG__);
}
~Test()
{
Console::WriteLine(__FUNCSIG__);
Dispose(false);
}
void Dispose()
{
Console::WriteLine(__FUNCSIG__);
Dispose(true);
GC::SuppressFinalize(this);
}
private:
void Dispose(bool disposing)
{
Console::WriteLine(__FUNCSIG__);
if (!this->disposed)
{
if (disposing)
{
Console::WriteLine(S"Disposing managed resources");
}
Console::WriteLine(S"Cleaning up unmanaged resources");
}
disposed = true;
}
bool disposed;
};
I was able to execute Test::Dispose() without any problems. I complied this first in 2003 and did the wizard migration to 2005. The resulting command line:
/O2 /D "WIN32" /D "NDEBUG" /D "_VC80_UPGRADE=0x0710" /D "_MBCS" /FD /EHa /MD /Yu"stdafx.h" /Fp"Release\McppOld.pch" /Fo"Release\\" /Fd"Release\vc80.pdb" /W3 /nologo /c /Zi /clr:oldSyntax /TP /errorReport:prompt /FU "c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\mscorlib.dll" /FU "c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.dll" /FU "c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Data.dll"
Geo
-- modified at 19:25 Thursday 18th May, 2006
|
|
|
|
|
Hello all,
How to generate class diagrams from VS 2005 .net for C++ project?
Thanks and Regards
Madhu
|
|
|
|
|
If you have Microsoft Visio, you can create UML diagrams.
|
|
|
|