|
Since you're asking on a managed-code board...
There's some built-in stuff already available to you:
Assembly Security Considerations[^]
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Hi all,
I have a problem with dialogs...
After using ShowDialog() method to show a dialog, memory usage (in Task manager) rises
Up about 3 Mega bytes. But after closing that dialog only 1 mega byte of memory releases.
I don't know how to free the extra memory that a dialog makes after closing it!
please help me...
Thank you.
Every new thing you learn,Gives you a new personality.
|
|
|
|
|
Call Dispose method of the form. Or just use using block and it will be automatically called for you.
|
|
|
|
|
Giorgi's reply is correct, but since this is a C++ board, I'll
assume you're using C++/CLI...
You should call Dispose on IDisposable-derived objects. More
important than releasing memory is releasing unmanaged resources
held by the object.
In C++/CLI, these are the ways to get Dispose() called on an object
(taken right from the docs):
*If an object created using stack semantics goes out of scope. For more information, see C++ Stack Semantics for Reference Types.
*If an exception is thrown during the object's construction.
*If the object is a member in an object whose destructor is running.
*If you call the delete Operator (C++) on a handle (^ (Handle to Object on Managed Heap)).
*If you explicitly call the destructor.
Using delete is the most natural C++ way IMO...Here's an example:
Form ^frm = gcnew Form();
...
delete frm;
You're still not going to see all memory released using task manager.
There will still be many managed objects that will be released when the
GC gets around to it.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Adding to other replies
You can use Stack Semantics to automatically delete object when the scope ends. So don't use handle when creating object. Write like this
Form2 frm;
frm.ShowDialog(); When variable goes out of scope, it's distructor will get called automatically.
|
|
|
|
|
What everyone else said, plus:
dSolariuM wrote: After using ShowDialog() method to show a dialog, memory usage (in Task manager) rises
Up about 3 Mega bytes. But after closing that dialog only 1 mega byte of memory releases.
What is going on in that dialog? If code in the dialog is allocating memory or resources then you are responsible for cleaning it up. Simply closing the dialog will NOT free any memory or resources that your code specifically allocates, including the dialog object itself if you allocated it on the heap.
led mike
|
|
|
|
|
Hi everybody.
I am currently working on a tool which is using satellite assemblies (.resx) string tables to support localisation.
Additionally, i would like to change the Culture manually by choice.
While changing the current language inside the program is working fine when started in Visual Studio (2008),however, it doesn't work in the release (tested on a vmware machine).
Well here is an example code, maybe someone can give me an hint why it works in Vs and not outside.
private: System::Void pictureBox1_Click(System::Object^ sender, System::EventArgs^ e) {
String ^loc;
loc = "de-DE";
wechsleSprache(loc);
}
private: System::Void wechsleSprache(String^ loc) {
Thread::CurrentThread->CurrentUICulture = gcnew System::Globalization::CultureInfo(loc, false);
Thread::CurrentThread->CurrentCulture = Thread::CurrentThread->CurrentUICulture;
this->Controls->Clear();
InitializeComponent();
this->textboxCurrentUICulture->Text = System::Globalization::CultureInfo::CurrentUICulture->Name; }
cheers,
joerg
|
|
|
|
|
jreisslein wrote: it doesn't work ... maybe someone can give me an hint
Hint? Good idea, maybe you can give us a hint about what "it doesn't work" means?
led mike
|
|
|
|
|
it doesn't work means:
when running inside VS, the program changes the language when clicking on the pictureBox. However it won't do when trying on a compiled version.
hope you understand now
|
|
|
|
|
if anyone is interested, i found the reason for this behaviour.
the local translation are all placed in satellite assemblies (.resx) files, and in a compiled version they are expected to be in subfolders of the working directory.
e.g. the translations for the de-DE Culture should be in ./de-DE/<assembly.dll>
Somehow the Setup Routine didnt include those assemblies and therefore the program could not find em and did fall back to its default language.
cheers
|
|
|
|
|
So it had nothing to do with C++/CLI. Glad you got it working and thanks for posting back the outcome.
led mike
|
|
|
|
|
Hi All,
I encoutered an issue in my program that uses .net serial port component. The scenario is as below:
1. I have developed my program and it works fine in "debug mode".
2. When I compile and run it in "Release mode", something wrong happened to the program. Some of my program logic
is no longer working as in "debug mode".
3. After performing some debug, I found out that the problem is I am using a while loop (pooling) on a flag
'AckReceived', and the flag is in an inconsistent state in 'release mode'.
Brief description on my serial port program design,
1. Main thread send a packet of serial data through serial port to external device, and expect an acknowledgement
within an interval, or else timeout will occur.
2. After sending data, main thread will perform a while loop to pool the 'AckReceived' flag. If it is true or
timeout occur, jump out from while loop.
3. When data is received at serial port, my receive event handler (port_DataReceived) will be fired to process the
serial data. The delegation is assigned as below:
objSerialPort->DataReceived+= gcnew SerialDataReceivedEventHandler(this,&CSerialPort_Driver::port_DataReceived);
4. When complete parsing the data, if a full serial packet is received and verified ok, 'ACKReceived' flag will be
set to true.
**Remark: Parsing and setting the flag 'AckReceived' is all done in the event handler port_DataReceived.
5. When 'AckReceived' flag is set to true, my main thread will jump out from while loop and continue the remaining
logic.
Problem:
1. In 'release mode', even though serial packet has been received successfully, and the 'AckReceived' flag is set
to true, the WHILE loop in main thread will NEVER jump out until timeout. This means, in main thread, it always
see the 'AckReceived' flag as false.
2. The logic works fine in 'DEBUG mode'.
3. My temporary solution is to put a System::Threading::Thread::Sleep(5) in the WHILE loop to keep the WHILE loop
checking at lower frequency, and this make the 'release mode' works!!
Obviously, the temporary solution is not a perfect one, but it does give some clue that timing is a problem in
'Debug' & 'Release' mode.
However, I couldn't figure out what is the exact problem and debugging in 'relese mode' is very hard and I am
helpless now. I am new in .NET development and not really sure on the threading model of .NET serial port, hence
not really sure if this issue is caused by syncronization problem of the 'AckReceived' flag?
Please can anyone explain to me what happen here and if possible share with me some possible solution?
Thanks in advance!
Development environment:
1. Language: Managed C++
2. IDE: Visual Studio.NET 2005
3. Serial port component: Version 2.0.0.0
Best Regards,
Winson My
|
|
|
|
|
Debugging in release mode is easy, just make sure you get the compiler to produce the debug symbols (you can do this for release builds) and then debug as normal.
regards,
|
|
|
|
|
|
Hi Everyone,
I am working on a project on VC++ 2008. It requires to generate reports based on predefined templates.
I found lot of information on how to attain this thru c#, but am not able to find out easy ways to do this in VC++ using .Net framework.
Any suggestion will be a great help to my project.
Thanks everyone,
Kasi
|
|
|
|
|
krish99 wrote: Any suggestion will be a great help to my project.
Try asking a specific question.
“Cannot find REALITY.SYS...Universe Halted.”
~ God on phone with Microsoft Customer Support
|
|
|
|
|
Hi,
Is it possible to do type casting implicitly, i.e. when making assignments, in managed c++.
In unmanged code it seemed good enough to overload the type cast operators for this to happen but it appears that this is not the case in managed c++.
I know that c# has an implicit keyword, is there an equivalent for c++?
Basically I want like to make an assignment from a number contained in my class to an int, bool or double without explicitly declaring this conversion.
Hope you can help out,
m.
|
|
|
|
|
mike toon wrote: In unmanged code it seemed good enough to overload the type cast operators for this to happen but it appears that this is not the case in managed c++.
Do you have an example where this isn't the case?
This still works in C++/CLI:
ref class numericstring
{
String ^str;
public:
numericstring()
{
str = gcnew String("123.456");
}
operator bool() {return Convert::ToDouble(str) != 0;}
operator int() {return Convert::ToInt32(Convert::ToDouble(str));}
operator double() {return Convert::ToDouble(str);}
};
[STAThreadAttribute]
int _tmain()
{
numericstring ns;
bool nsbool = ns;
int nsint = ns;
double nsdouble = ns;
...
This form works as well
ref class numericstring
{
String ^str;
public:
numericstring()
{
str = gcnew String("123.456");
}
static operator bool(numericstring^ val) {return Convert::ToDouble(val->str) != 0;}
static operator int(numericstring^ val) {return Convert::ToInt32(Convert::ToDouble(val->str));}
static operator double(numericstring^ val) {return Convert::ToDouble(val->str);}
};
[STAThreadAttribute]
int _tmain()
{
numericstring ^ns = gcnew numericstring();
bool nsbool = ns;
int nsint = ns;
double nsdouble = ns;
...
In both versions above, implicit is implied, but you can add the explicit
keyword to force explicit conversion semantics (which in the above examples
would be a good idea )...
ref class numericstring
{
String ^str;
public:
numericstring()
{
str = gcnew String("123.456");
}
static explicit operator bool(numericstring^val) {return Convert::ToDouble(val->str) != 0;}
static explicit operator int(numericstring^val) {return Convert::ToInt32(Convert::ToDouble(val->str));}
static explicit operator double(numericstring^val) {return Convert::ToDouble(val->str);}
};
[STAThreadAttribute]
int _tmain()
{
numericstring ^ns = gcnew numericstring();
bool nsbool = (bool)ns;
int nsint = (int)ns;
double nsdouble = (double)ns;
...
See Changes to Conversion Operators[^]
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Hi thanks for the help. The problem making assignments after the initial declaration.
e.g.
double nsdouble = ns;
nsdouble = ns; //complition error here.
Error 1 error C2678: binary '=' : no operator found which takes a left-hand operand of type 'double' (or there is no acceptable conversion)
I tried to strip down my code into a new class ( to post ) and lo and behold it compiled and operated as should be.
So i'm baffled where the problem might be.
modified on Wednesday, October 29, 2008 7:45 AM
|
|
|
|
|
mike toon wrote: i'm baffled where the problem might be.
Me too.
There's no reason that shouldn't compile, and it compiles fine
in both examples I gave.
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Yeah. Sorry i didn't communicate that well. Your code works and compiles fine.
I think i've figured out the problem, but not entirely sure why it occurs.
In addition to the cast overloads i also have an assignment overlaod which uses a template.
If i remove this template then it compiles fine, however with the template it causes the said error.
using namespace System;
ref class Value
{
public:
enum struct ValType
{
Empty,
Bool,
Double,
Integer
};
Value();
Value^ operator=( Value^ rhs );
Value( bool value );
Value( double value );
Value^ operator=( double doubVal );
Value^ operator=( bool boolVal );
template<typename t="">
Value^ operator=( T value )
{
m_type = ValType::Integer;
m_value = static_cast<int>( value );
return this;
}
operator double( );
operator bool( );
private:
Object^ m_value;
ValType m_type;
};</int></typename>
Any ideas why this compilation error occurs?
Many thanks,
mike.
|
|
|
|
|
Just found out that you can't overload the type cast () in .net which has made this attempt rather pointless. I had been compiling a dll library in managed c++ which was to be used by c# code. Any clues as how I can provide the functionality to do the type casting implicitly with .net?
thanks,
Mike
|
|
|
|
|
mike toon wrote: Any clues as how I can provide the functionality to do the type casting implicitly with .net?
Sure. Use implicit[^]
led mike
|
|
|
|
|
mike toon wrote: I had been compiling a dll library in managed c++ which was to be used by c# code.
In that case you should be using the second version of my sample -
the managed version (as also shown in led mike's link).
Like I mentioned, the operators in that form are implicit unless explicitly set
explicit with the keyword explicit.
using namespace System;
namespace TestCPlusPlusClassLibrary
{
public ref class numericstring
{
String ^str;
public:
numericstring()
{
str = gcnew String("123.456");
}
static explicit operator bool(numericstring^ val) {return Convert::ToDouble(val->str) != 0;}
static explicit operator int(numericstring^ val) {return Convert::ToInt32(Convert::ToDouble(val->str));}
static explicit operator double(numericstring^ val) {return Convert::ToDouble(val->str);}
};
}
...
TestCPlusPlusClassLibrary.numericstring ns = new TestCPlusPlusClassLibrary.numericstring();
bool nsbool = ns;
int nsint = ns;
double nsdouble = ns;
...
Note how WITH the "explicit" keyword in the example above, the C# will not compile.
Without the "explicit" keyword, it compiles and runs fine.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
modified on Thursday, October 30, 2008 3:20 PM
|
|
|
|
|
Thanks to both of you for the help, this works fine now.
Is it possible to over-ride the '=' operator as well, can this be declared as a static function in the same way?
I get this error if i try this:
error C2581: static 'operator =' function is illegal
According to msdn '=' operators cannot be static.
Oops, answer was already contained in that blog article
Cheers again.
|
|
|
|