How to clean up memory in WT? - c++

UPDATE 3/27/2013
It would appear that I am not leaking memory, it is just WT not keeping a persistent session every time F5 is hit, or a new user connects. Basically the old session gets deleted, and a new one is made every time F5 is hit, or a new user connects from another machine. I have read some parts of the documentation that mention making the session persistent, so when a user reloads the page, or a different user connects they all see the same content. However, I have not been able to get it working yet. I think it is a function call or a setting in the wt_config.xml file. Will update if I make any other progress.
ORIGINAL POST
So my question is, how do I clean up memory in WT so every time the user presses F5 on the page the memory use stays the same in the task manager?
Ok, so I am working with WT pronounced (witty) and I have noticed that my server application consumes more memory every time the user hits F5 on the page to refresh it, which to me looks like I am leaking memory, but I followed the same process as WT most basic applications...
So, I went back to the most basic WT app I could find, the hello application the code for which, and the working example, can be found here(http://www.webtoolkit.eu/wt/examples/) if you have not personally built the project.
Once I ran the example on my machine and hit F5 on the page, the memory in my task manager increased.
My likely suspect is this function below.
WApplication *createApplication(const WEnvironment& env)
{
/*
* You could read information from the environment to decide whether
* the user has permission to start a new application
*/
return new HelloApplication(env);
}
It gets called every time F5 is hit and makes a new instance of the HelloApplication which inherits from WApplication.
Some things I have tried to remedy the situation that have not worked include: Keeping 2 pointers for the HelloApplication so I can delete the old pointer every time a new one is allocated. Calling the quit() function, and deleting the pointer. Just calling the quit() function. I have also looked around on the WT documentation site(http://www.webtoolkit.eu/wt/doc/reference/html/index.html) for more detailed information on the class and it's methods, but have not come up with anything that worked.
I ask that anyone responding please be as detailed as possible in how to handle the cleanup of the memory. An example would be much appreciated, thanks in advance!

You also must be aware of the fact that as of Wt 3.3.0 the sessions are only cleaned up as long as requests are received (see this reply of a Wt developer). To overcome this limitation the developer suggests using something similar to following code.
static bool terminating = false;
void
callRepeatedly(boost::function<void()> function, int seconds)
{
if (!terminating) {
Wt::WServer::instance()->ioService().schedule(
seconds * 1000, boost::bind(callRepeatedly, function, seconds));
}
function();
}
int
main(int argc, char** argv)
{
...
callRepeatedly(boost::bind(&Wt::WServer::expireSessions, &server), 60);
Wt::WServer::waitForShutdown();
terminating = true;
server.stop();
...
}

The manual of WApplication says that you create it when the createApplication callback is called, and that Wt deletes it when quit is called, and when the session times out. The default session time-out is 10 minutes (wt_config.xml), so that may be the reason why your memory consumption grows initially when pressing F5.
http://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1WApplication.html#details
Something different that explains what you see: memory consumption reported by the operating system is not a reliable method to determine if an application leaks memory since free does not really return the memory to the OS. Use proper memory checking tools, such as valgrind.

Related

My C++ Unity plugin runs once in the editor but not twice

I have a plugin that calls C++ code. When the game starts, it calls this extern C++ function:
void startPlugin() {
MyClass::instance = new MyClass();
MyClass::instance->process();
}
The process method runs an infinite loop that processes data and goes on, as long at the keepRunning member is true. Because it runs an infinite loop and blocks, this method is called within its own thread.
When the game ends I run this extern method from C#:
void stopPlugin() {
MyClass::instance->keepRunning = false;
sleep(1); // Make sure the process loop is done. It should take less than one frame, but just to be sure...
MyClass::cleanup();
}
Which refers to this method:
void MyClass::cleanup() {
delete instance;
instance = NULL;
}
Based on what the console says, these methods are indeed run.
On Mac, this works the first time. I can start the game in the editor and stop it in the editor and everything works fine. But if I try to run it a second time without restarting the Unity editor, the whole editor freezes. Sometimes it takes three times instead of two, but invariably the second or third attempt to play freezes Unity completely. The cursor changes to the spinning pinwheel.
The log says:
Starting.
(Filename: /Applications/buildAgent/work/d63dfc6385190b60/artifacts/MacEditorGenerated/UnityEngineDebug.cpp Line: 49)
Receiving unhandled NULL exception
Launching bug reporter
Obtained 37 stack frames.
#0 0x00000090019390 in _platform_memmove$VARIANT$sse42
And then the stack trace brings up the function that calls the extern function.
Trying to join the thread that was running startPlugin freezes the editor on stop. I don't think it should do that. After all, at that stage, the loop is over and the instance cleaned. This is not guesswork, there are visible side effects: ending the loop turns off the computer's camera and I can see the light of the camera go off. Also a debug message is sent in C# after the call to startPlugin returns. So there is no doubt that the loop is over.
A previous version that does not delete the instance works every time on Windows.
Am I doing something obviously wrong?
I found my answer.
Even though no thread is called inside of the C++, I still need call pthread_exit(this) at the end of the process method. This is not necessary on Windows, but it is on Mac. After that, all works.

C++ : Running a background task all the time on machine

I want to make an MFC app which leave a thread/process running in the background all the time that keeps track of something like hard disk size.
Whenever the hard disk size goes beyond lets say 90% it shows a warning dialog (also MFC dialog of same app).
I am not sure how to do it.
I tried the windows service option, but it doesn't seem much reliable to me , as most of the times, the service is not successfully installed, or if installed it doesn't get started successfully.
What other options do I have to achieve it?
Any help is appreciated.
Create a worker thread which keep on monitoring the disk space.
Create a user defined message in the main thread and provide a handler for it
When disk space goes more than 90%, Post a message (Post the user defined message that you created)
From the main thread handler for the user defined message "Display the warning message"
Note: Services are not suitable for this task as they don't like user interactions.

MFC UI Automation graceful shutdown

Our MFC app hangs during shutdown if any UI Automation client is active (Such as Inspect. Windows Eyes, UI Spy etc.)
The reason is BOOL AFXAPI AfxOleCanExitApp() returns false if any Ole Objects exist. The app then goes into hidden server mode.
I have seen similar posts dealing with Document objects. The general solution is to set the object count to 0, close normally then set the count back in the OnClose of the main frame.
This is a poor solution for UI Automation. It causes memory leaks and invalid objects in the Client app ( Inspect actually crashes after a time).
Has anyone seen a proper way to tell UI clients this server is going away and release all objects?
There is no real good way to shut down graceful. There is no graceful way to stop any server when it is still in use. You can only do necessary cleanup.
You have Connections to you objects. What is graceful if you cut them? You can use CoDisconnectObject for every object. But there is no difference when you terminate the application. Also using this function doesn't reduce the objects lock count! But you can delete the object without getting a crash with an access from the other COM clients.
The draw back: CoDisconnectObject only works for external links. If you have internal COM pointers the object, they are not affected. So those may still use your object...
When you really find every object that has an external connection you can destroy it. And if you have no internal COM-pointers you can delete your objects even with a usage count !=0. But in lots of cases I have other dependent COM-objects that are linked...
The only real good way to terminate gracefully is to stop all applications that use your application as a server first! And exit after this is done... ;)
So if you want to force a shutdown. Disconnect what you can. Free as many resources you know. Than ignore the applications lock count and exit. Memory is freed, even if the debug version will report a leak. Problematic are only other resources (files, mutexes, system objects...) that may need a better handling as closing the application...

Qt Program Hangs (Not Responding) Until Function ends then starts working again

I have a UI application in Qt, i have a couple of functions that run large scale SQL queries that returns thousands of results.
when the button that runs this query is clicked the UI windows just instantly goes to 'not responding' however i can see from console outputs that everything is still actually running in the background. As soon as the function ends the data is presented as expected and the UI is responding again and fully functional.
i know this is due to the fact that the function is looping thousands of times due to the large number of results, but i was hoping that i could have just put in a loading bar that progresses as the search does instead of just locking up the window making it look like the program has crashed. AFAIK i dont have memory leaks so does anyone have any suggestions?
oh also im thinking its not memory leaks because when i click that button task manager shows only a couple of MB of memory being used for this process and processor is by no means maxing out either
In an application, there's one thread that's responsible for handling UI events, messages, whatever you want to call them. Suppose that you have a button click event. As long as you don't return from the callback function, no other UI event can be triggered (repainting, updating, etc) and UI becomes unresponsive.
To mitigate this, you should considering performing time consuming tasks in a separate thread and once they're complete, update UI accordingly. If you need to block UI while the task is processed, you can disable your controls, display a pop up progress bar, whatever, but keep the UI thread relatively unoccupied to avoid "not responding" problem.
A simpler solution than to use threads is to use QCoreApplication::processEvents(). If your code is something like this:
void slowFunction()
{
lostOfResults = makeSqlQuery(...); // quite fast
for (r in lostOfResults)
doSomethingWithResult(r); // one call is quite fast
}
If one SQL query or one doSomethingWithResult() doesn't take too much time, you can process pending events using QCoreApplication::processEvents() like this:
void slowFunction()
{
lostOfResults = makeSqlQuery(...);
for (r in lostOfResults)
{
doSomethingWithResult(r);
QCoreApplication::processEvents();
}
}
Now the GUI events are processed and the program doesn't freeze. But if the SQL query alone takes a lot of time (several seconds) this doesn't help. Then you should consider separate thread.

Is it possible to kill a C++ application on Windows XP without unwinding the call stack?

My understanding is that when you kill a C++ application through Task Manager in Windows XP, the application is still "cleanly" destructed - i.e. the call stack will unwind and all the relevant object destructors will be invoked. Not sure if my understanding is wrong here.
Is it possible to kill such an application immediately, without unwinding the stack?
For example, the application may employ RAII patterns which will destroy or release resources when an object is destructed. If the traditional "kill process" through Task Manager is graceful, providing a way to kill the application immediately would allow me to test ungraceful shutdown (e.g. a power outage).
Edit:
Just to clarify, I was after an existing utility or program that would allow me to do this. I should be able to use the solution on programs that I don't have the source code for, meaning that a programmatic solution is not really acceptable.
Edit:
Just to provide more context, sometimes I have to work with 3rd party services which are very intrusive (e.g. nagging me to reboot every hour). Since I know that I don't need to reboot, I want to kill the process/service so it doesn't nag me anymore. Unfortunately some of the 3rd party developers were "smart" enough to prevent me from doing this, and when I kill the process through Task Manager, the system will reboot immediately (I'm guessing that are using RAII to achieve this).
I believe task manager tries a "nice" shutdown by sending a WM_CLOSE message, then if the application doesn't respond it's killed.
This call should kill the process immediately with no warning:
TerminateProcess
e.g.:
TerminateProcess(GetCurrentProcess(), 1);
Update:
You may find this article interesting:
Quitting time: exiting a C++ program
Update 2:
I should be able to use the solution on programs that I don't have the source code for
Hmm, well this is undesirable behavior 99.9% of the time.
SysInternals has a utility called pskill:
http://technet.microsoft.com/en-us/sysinternals/bb896683.aspx
but I'm not sure how "nice" it is.
You might need to roll your own, but it should be pretty easy:
DWORD pid = <get pid from command line>;
TerminateProcess(OpenProcess(PROCESS_TERMINATE, FALSE, pid));
The standard Windows way to do this, without relying on 3rd-party tools, is to use taskkill /f:
taskkill /f <process-id>
taskkill /f /im <process-executable-name>
/f means "force" here, and ensures that process is terminated unconditionally and immediately, with no query or warning.
Unless I'm terribly mistaken (and I just did a little testing to confirm), Task Manager tries to close programs in different ways depending on which tab you're using. If going through the Applications tab and pressing End Task, it will try to close the program cleanly by first sending a WM_CLOSE. But if going through the Processes tab and pressing End Process, it seems to use something along the lines of TerminateProcess, which means no stack unwinding and such.
So first, if you aren't using End Process on the Processes tab, try that.
If that's what you already tried and their software still manages to reboot the system somehow, then there is something more complicated going on. Other people may be on the right track about there being additional processes.
I believe the C standard library method exit(0); will do exactly that, abort the program without calling any destructors, deallocators, etc.
Try that, and let me know if it meets your needs?
It looks like abort() will give you an abnormal exit.
ANSI 4.10.4.1 The behavior of the abort function with regard to open and temporary files
The abort function does not close files that are open or temporary. It does not flush stream
buffers
[source]
and
Abort current process
Aborts the process with an abnormal program termination.
The function generates the SIGABRT signal, which by default causes the program to terminate >returning an unsuccessful termination error code to the host environment.
The program is terminated without executing destructors for objects of automatic or static
storage duration, and without calling any atexit function.
The function never returns to its caller.
[source]
I would try PSKill as suggested by Tim above. I would guess that this will fail as well. If the 3rd party services are really serious about avoiding death, then the service definition may be set to "reboot on crash". The other common approach is to have another service that watchdogs the primary one. The primary service usually sets a global event or employs some other notification mechanism that the watchdog service watches. If the primary service doesn't notify the watchdog, then the watchdog restarts the computer.
The aptly named Kill Tool, available from Microsoft Download. Is part of the Windbg suite also.
The Kill tool, kill.exe, terminates
one or more processes and all of their
threads. This tool works only on
processes running on the local
computer.
kill /f <process>
For example, kill /f lsass (just kidding, do not kill LSA!).
If you want to roll your own, TerminateProcess is the way to go.
The C function abort() in the standard library will instantly kill your application with no cleanup.
C++ defines a standard global function terminate(). Calling it will also instantly exit your application.
Technically terminate()'s behavior could be overridden by the set_terminate function. It calls abort by default.
There are utilities around that can forbid reboot.
HideToolz does that for example -- there is a checkbox buried somewhere that will make it ask you when something initiates reboot. It is detected by many antiviruses as rootkit (which it is, but this one is supposedly tame), so it might be probematic to run on systems you don't have full control over (when antivirus mandated by domain policy, etc)
Extending Pavel's answer:
HANDLE launch(string filename, string params)
{
auto ftemp = wstring(filename.begin(), filename.end());
LPCWSTR f = ftemp.c_str();
auto ptemp = wstring(params.begin(), params.end());
LPCWSTR p = ptemp.c_str();
SHELLEXECUTEINFO ShRun = { 0 };
ShRun.cbSize = sizeof(SHELLEXECUTEINFO);
ShRun.fMask = SEE_MASK_NOCLOSEPROCESS;
ShRun.hwnd = NULL;
ShRun.lpVerb = NULL;
ShRun.lpFile = f;
ShRun.lpParameters = p;
//ShRun.nShow = SW_SHOW;
ShRun.nShow = SW_HIDE;
ShRun.hInstApp = NULL;
if (!ShellExecuteEx(&ShRun))
{
//Failed to Open
}
return ShRun.hProcess;
}
void kill(string filename)
{
launch("taskkill.exe", "/f /im " + filename);
}
void main()
{
kill("notepad.exe"); //Kills all instance of notepad
}