|
I understand that some things will be held on to because they don't go out of scope. But it seems to me there are some serious problems within the .NET framework not properly releasing references. Consider the behavior of the Menu control:
Every time I click my "File" menu (just as an illustration), MORE memory is sucked up to generate the drop down (New, Open, Save, etc...). Sure, the Menu control never goes out of scope as long as the application is run, but why does it grab more memory every time? It seems to me that after the memory has been allocated once, that should be the end of it. If I call up the "File" menu a second time, there should be no reference left to the original fly-out. The memory consumption should stay constant because one reference was destroyed and one was created. But no. The framework sucks up the additional memory to create a new reference without reliquishing the old reference.
As many times as I expand any menu item, more memory is consumed. None is ever reliquished. And, as I stated in my original post, I took my unmanaged data out of the equasion. I'm talking about just running an application form with a menu system -- before I do any of the real application work or allocate any of the core data structure memory.
If .NET won't release any of the consumed Menu memory before application termination, then I might as well go back to VS6. That's just too sloppy.
|
|
|
|
|
AFAIK, the GC will kick-in whenever it feels like working or when a low memory situation. In my little world, I have never had a .NET application run out of memory that they gobbled up!
"We make a living by what we get, we make a life by what we give." --Winston Churchill
|
|
|
|
|
Like I mentioned to led mike, I don't use .NET GUI so I can't comment on the
specifics of your situation..
If I remember correctly from my tests, in an infinite loop allocating objects on the managed heap
and watching in the task manager, memory use would climb to around 300MB between each cleanup
by the GC. That was VS2003/.NET 1.1. Strangely, I cannot replicate that with VS2005 LOL
I'm trying to make an infinite loop that allocates and the memory never moves.
If you need to keep everything cleaned up immediately, Does GC.Collect() help?
If this stuff leaks why isn't everyone screaming about it? I would think by now
someone would have noticed. .NET would indeed be useless.
Cheers!
MArk
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Also, you can minimize memory usage in .NET by cutting back in the creation of mulitple or temporary small objects. For example, instead of doing a lot of string manipulations on a String , use a StringBuilder object instead.
"We make a living by what we get, we make a life by what we give." --Winston Churchill
|
|
|
|
|
I am aware of a few optimizations such as the use of StringBuilder. The behavior I noted is more apparent in items that are beyond my control -- such as the Menu control. Why should more memory be allocated every time a fly-out is invoked, yet when the fly-out disappears no memory is ever released?
However, I posted a similar thread on another board, and one of the respondants said that I'm looking at this all wrong. I have only Task Manager to gauge memory use, but he said I can't do that because a .NET application runs inside of a "virtual machine". Therefore Task Manager shows only what the VM is reserving, not necessarily what I'm using.
I only half-way buy this idea. Regardless whether .NET is running a VM or not, the fact is that every time I invoke a menu, or a dialog box, the Task Manager numbers go up. The respondant's point was that the number in TM will NOT go down because the VM has reserved that memory. Well fine. But if the memory is reserved, they why does it need to reserve even MORE memory the next time I trigger a fly-out menu? The memory was already reserved once. If the memory was truly freed after the fly-out disappeared the first time, the virt. mach. should not need to compound its memory reservation.
From that angle, it seems to me that Task Manager is indicating there is something wrong with .NET's virt. machine. BUT Mark has a very valid point that if there was truly something to be worried about, it seems like there would be a lot more people screaming about it.
The overwhelming opinion on this and other message boards is that I'm worried about nothing. I guess I don't know enough about .NET internals. All I know is that the numbers I saw in Task Mgr don't make a lot of sense, so I thought I'd inquire about it.
|
|
|
|
|
Your concern is quite natural and valid. I do not like to see wasted resources. However, I think the ultimate goal of the GC is efficiency over resource conservation. If the GC is constantly reclaiming and defragmenting memory, your application speed will suffer. You, as a programmer, gain freedom from resource management, but the trade off is this management is in a black box, and various opinions on what is going on in this black box.
"We make a living by what we get, we make a life by what we give." --Winston Churchill
|
|
|
|
|
I am trying to put information in an access database file (mdb) with an oledb connection. Everything seems fine but no new information is added to the database at the end.
This is the code I have now:
double Tabel_Panelinfo(String^ item_id, String^ use, String^ type)
{
int dblID = 10;
OleDbConnection^ myOleDbConnection = nullptr;
try
{
OleDbConnectionStringBuilder^ connectionBuilder = gcnew OleDbConnectionStringBuilder();
connectionBuilder->Provider = "Microsoft.Jet.OLEDB.4.0";
connectionBuilder->DataSource = "D:\\Data\\Visual Studio 2005\\Projects\\VertexBDXML-RandekSPL728\\VertexBDXML-RandekSPL728\\Tabel-XML.mdb";
myOleDbConnection = gcnew OleDbConnection(connectionBuilder->ToString());
myOleDbConnection->Open();
OleDbDataAdapter^ DataAdapter = gcnew OleDbDataAdapter("SELECT * FROM tblPanel", myOleDbConnection);
DataAdapter->FillError += gcnew FillErrorEventHandler(MyEventHandler::FillError);
DataAdapter->RowUpdated += gcnew OleDbRowUpdatedEventHandler(MyEventHandler::RowUpdated);
DataSet^ myDataSet = gcnew DataSet();
DataAdapter->MissingSchemaAction = MissingSchemaAction::Add;
DataAdapter->Fill(myDataSet, "tblPanel");
DataRow^ row = myDataSet->Tables["tblPanel"]->NewRow();
if (row != nullptr)
{
row["item_id"] = item_id;
row["use"] = use;
row["type"] = Convert::ToInt16(type);
DataAdapter->Update(myDataSet, "tblPanel");
}
}
catch(Exception^ e)
{
Console::WriteLine(e->Message);
}
finally
{
if ( myOleDbConnection != nullptr )
myOleDbConnection->Close();
}
return dblID;
}
Stefan
|
|
|
|
|
This is the managed C++ forum (as clearly stated on this page) and therefore the wrong forum for this question. Make sure you are using some sort of tutorial or example, there are many database articles here on code project (use the site navigation ). When you repost your question in the correct forum (Visual C++ / MFC ) be sure to reference the article or tutorial you are working from.
|
|
|
|
|
he's in the right forum, he's clearly using C++/CLI and not native C++.
|
|
|
|
|
ooops
apparently I have been brainwashed. I went off the subject.
|
|
|
|
|
|
I might be the only member stupid enough to pull that off!
Happy Friday... I'm out of here
|
|
|
|
|
See if adding the row to the table does it.
myDataSet->Tables["tblPanel"]->Rows->Add(row);
|
|
|
|
|
This still does not work, i added it in between the other code.
I now get exception for e { "Index was outside the bounds of the array."}
Is there maybe a good example to write information to an access database using C++.
I have found many examples but cannot find any that works in my project.
DataRow^ row = myDataSet->Tables["tblPanel"]->NewRow();
myDataSet->Tables["tblPanel"]->Rows->Add(row);
if (row != nullptr)
{
row["ID"] = dblID;
row["item_id"] = item_id;
row["use"] = use;
row["type"] = Convert::ToInt16(type);
DataAdapter->Update(myDataSet, "tblPanel");
}
Stefan
|
|
|
|
|
I finally found the error that prevented me from adding the data.
I have used this code:
DataTable^ MyTable = myDataSet->Tables[0];
DataRow^ row = MyTable->Rows->Add();
instead of this:
DataRow^ row = myDataSet->Tables["tblPanel"]->NewRow();
Stefan
|
|
|
|
|
Is there a way for me to create a listview hearder spanning 2 rows using C/C++. If there is, then I would appreciate it if someone can provide me with some code snippit.
|
|
|
|
|
try inserting a newline character in header name
|
|
|
|
|
I tried that and it didn't work.
|
|
|
|
|
|
I am not creating an MFC app, just a windows app. I am using the LVCOLUMN structure to create my listview.
|
|
|
|
|
Ok.
The point was, you can use custom draw to achieve this.
It's no different with MFC than without MFC, except where you handle the messages.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Hi again, I am using Visual Studio 2005 C++/CLI. I have a form that I want to display statistical information. There is a TextBox control on the form named textBox1. When I click on the menu selection to display the statistics form the following code is executed...
private: System::Void waferMapStatisticsToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e)
{
Stats^ statistics = gcnew Stats(); // instantiate a new statistics Form object
TextBox^ text_box = gcnew TextBox(); // instantiate a new TextBox object
text_box->Text::set("This is a test");
statistics->textBox1 = text_box; // this does set the value of textBox1 to "This is a test"
statistics->Text::set("DIALOG TITLE STRING"); // this does change the forms title
statistics->ShowDialog();
}
If I step through the code I can see that the value of the textBox1 control on the statistics Form changes, but when I ShowDialog() the statistics Form title changed but there is nothing displayed in textBox1. I have also tried using statistics->Update() and have tried setting the value of textBox1 through a property definiiton. The results are the same. This seems to be trivial and intuitive but alas. Does anyone know why the textBox1->Text is not displaying on the statistics Form?
Thanks
Buck
|
|
|
|
|
Hi Buck,
I don't know what statistics and statistics->textBox1 are.
My best guess is statistics is some kind of Form, and textBox1 is a public field,
of the TextBox^ kind.
If so the question is when do you add textBox1 to the statistics->Controls?
if you do that before you execute statistics->textBox1 = text_box;
then the original textbox is included in your form, and not the one you
intend to substitute just before the ShowDialog().
The solution would be to make textBox1 private (it is not a good idea to
make Controls public, only some aspects of it need to be public at most),
and to provide another public property to set its text.
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips:
- make Visual display line numbers: Tools/Options/TextEditor/...
- show exceptions with ToString() to see all information
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
Hi Luc,
Yes, statistics is a Form and textBox1 is a public field of the TextBox^ kind. I have tried this with textBox1 private and accessed through a public property with the same results. If I do statistics->Controls->Add(text_box), a TextBox control appears (where I dont want it) on the form when ShowDialog() is called. But I don't want to add a control, I want to change the text of an existing control. The Form designer in Visual Studio allows me to select TextBox controls from the toolbox and place them on the Form. The default names as they are placed on the Form would be textBox1, textBox2, etc, so these TextBox(es) already exist on the Form (I shouldn,t have to Add() them). All of the docs that I have read simply show changing the TextBox->Text:: property to whatever the text should be. And when I do that you can see (when executing the code one line at a time) the value of the TextBox changes to the text that I want it to be. It just does not appear when the Form is displayed (the TextBox control appears, just with no text in it).
Buck
|
|
|
|
|
Okay, this is the kind of crap that just drives me insane. Once I added the line 'statistics->Controls->Add(TextBox);' and saw that I got a TextBox where I did not want it, then everything began to behave the way I expected to in the first place. In other words, the code snipet in my original posting that would not display the text is NOW DISPLAYING THE TEXT! Lovely, I already have a ghost in my Microsoft machine, what else is new.
Buck
|
|
|
|