I have a Database Class which gets created on the CLR heap using gcnew. The problem is that the database connection is made in the constructor of the database class. What I need to do is deallocate the database object early if the connection to the database is lost and then recreate the object on the CLR heap. My question is can memory allocated with gcnew be deallocated before the garbage collector deallocates it using the delete operator?
My question is can memory allocated with gcnew be deallocated before the garbage collector deallocates it using the delete operator
No. delete on a managed object won't remove the instance from CLI heap. All it does is call the destructor of the object. GC will reclaim the memory later.
You can cleanup the resources that you have used, like connection object or any other native handles deterministically. But the object will be deallocated only by garbage collector. On a garbage collected environment, you don't have to worry about cleaning up the memory allocated. All you have to ensure is no strong references exists to the object that will prevent GC from collecting.
For cleaning the resources, C++/CLI provides methods like "Stack semantics". In which the compiler implements IDisposable on the type and create code to call Dispose() method when the scope ends. You can write your cleanup code inside the Dispose() method. This is the recommended way of cleaning up managed objects. For native objects, you have to call delete on them.
John Robert Wilk wrote:
The problem is that the database connection is made in the constructor of the database class
Usual pattern for database access is, opening connection, executing query and closing the connection. ADO.NET has connection pooling built in and connection instances are created from there. Unless you have a good reason for initializing the connection on the constructor, the pattern explained should be followed.
Yes. By default compiler treat it as managed integer. And what type of interger is implementation defined. On a 32 bit machine you could get System.Int32 and Int64 on 64 bit machines. Now consider the following example
int x = 3;
std::cout << x;
std::cout expects a native integer and you are passing a managed one. So the compiler does the conversion for you.
and what about char ch ?
This will be compiled as System.SByte.
We see that int arr is a native array
int arr="Hi"; is an invalid statement. But in general, this is treated as a managed CLI array.
The last question Wink ,, For both the managed array ManArr1 & ManArr2 what is the difference between the two initialization ??
If you want to use dynamic_cast, it seems that you must use a template instead of a generic. That is by replacing generic<class WindowType> where WindowType : MyWindowBase with template<class WindowType>
This does make some sense since generics are run-time and templates are compile-time and dynamic_cast need to know T type at compile time.
Every body know this question answer please send me the reply?
In visual studio 6.0 .Debugging operation performed press F10 it takes to the next step into host information(means takes assembley) in windows 7 64-bit environment.
As far as I know, safe_cast is more like dynamic_cast (which you can also use on managed types). safe_cast and dynamic_cast are for casting between handles to polymorphic types within a class hierarchy (much as you would do in C++ with pointers). The difference is that safe_cast throws an exception when the cast is not possible rather than returning nullptr. This means you don't have to write code that checks to see whether the cast succeded, just handle the exception, meaning potentially less cluttered code.
Just wondering how and if you can pre-select multiple files as the dialog opens? Do you place a list of them in the Initial Filename area, as I've tried this already the answer seems to be, No.
Am I missing something?
Basically I'd like a user to be able to select multiple files, I then do some checking of the filenames and if one of these checks comes back false, I want to reload this dialog with the files that the user had previously selected.
I've a file called test.ini. The contents of this file is:
I've to read the file and assign each value after the equal(=) sign to three System::String^ variables. I don't want the text preceding the equal sign(i.e PreviousCheck =,LID = and PWD = all I don't want). How to do this in C++/CLI?
I used the following code to get a line from the file(and then I will split it into parts).
System::IO::StreamReader reader("test.ini", System::IO::FileMode::Open);
System::String^ line = reader.ReadLine();
But I got the following error:
error C2664: 'System::IO::StreamReader::StreamReader(System::IO::Stream ^,bool)' : cannot convert parameter 1 from 'const char ' to 'System::IO::Stream ^'
Reason: cannot convert from 'const char *' to 'System::IO::Stream ^'
No user-defined-conversion operator available, or
Cannot convert an unmanaged type to a managed type
I think this is a subtle one; you have provided a char* in parameter 1 of the constructor, but no such constructor exists. You need to use a String object as the path parameter for the compiler to select the correct constructor. You can find all the gory details here[^] on MSDN.
I must get a clever new signature for 2011.
Last Visit: 31-Dec-99 18:00 Last Update: 25-Sep-23 7:56