我的一个可执行文件在单独运行时打开了大约 330 个句柄。当它与另一个特定进程结合运行时,它会泄漏许多句柄。
我使用了 sysinternals 的“句柄”实用程序来检查在这两种情况下所有句柄都打开了什么。当此进程与其他特定进程结合运行时,它具有以下句柄条目的额外内容。
578: Process
57C: Thread
580: Process
584: Thread
588: Process
58C: Thread
590: Event
598: Process
59C: Thread
5A0: Process
5A4: Thread
5A8: Process
5AC: Thread
5B0: Process
5B4: Thread
5B8: Event
Run Code Online (Sandbox Code Playgroud)
这样它就为进程、线程、事件打开了 400 个额外的句柄。最终,这种泄漏导致应用程序崩溃。
我是 Windows 编程的新手,请原谅我问的是非常基本的问题。我将非常感谢这方面的任何帮助/建议。
我正在编写一个小通知服务器来将数据推送到客户端。基本架构看起来像这样(精简伪代码):
acceptConnections sock = forever $ do
connection <- accept sock
forkIO (handleConnection connection)
handleConnection connection = do
connectionHandle <- socketToHandle connection ReadWriteMode
handleMessage connectionHandle
hClose connectionHandle
handleMessage connectionHandle = forever $ do
message <- hGetLine connectionHandle
if shouldPushMessage message
then hPutStrLn targetConnection message
else return ()
Run Code Online (Sandbox Code Playgroud)
其中 targetConnection(在 handleMessage 中)来自一个单独的连接,并在不同的线程中挂起 handleMessage,等待其缓冲区被填充。我认为这会导致问题,因为我有 2 个线程访问同一个句柄。所以,我的问题是,为什么这不是问题?或者是这样,我只是还没有看到它变成一个问题?在我的实际应用程序中,当我获取 targetConnection 时,我是通过我通过 MVar 访问的地图来实现的,但是在 hGetLine 调用中并没有安全地访问它。
免责声明:我是一个完整的 Haskell 和多线程新手
感谢您的任何解释/见解!
按下后退按钮时,我不希望我的活动被破坏.我的应用程序与1.6 SDK兼容.参考http://android-developers.blogspot.com/2009/12/back-and-other-hard-keys-three-stories.html和覆盖后退按钮以像主页按钮一样,我选择了以下代码:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// For versions lower than 2.0
if (Utility.buildDet.getDeviceBuildAPI() <= Utility.buildDet.getBuildApi()
&& keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
onBackPressed();
return super.onKeyDown(keyCode, event);
}
// In any version, this function will be called
public void onBackPressed() {
// This will be called either automatically for you on 2.0
// or later, or by the code above on earlier versions of the platform.
Log.i(TAG, "##### BACK KEY IS …Run Code Online (Sandbox Code Playgroud) 我有一个内核驱动程序.如何在内核驱动程序中枚举指定进程的所有打开句柄?我想关闭这些句柄.
谢谢!
在使用win32 API方面,我是一个新手,所以请多多包涵。
我目前正在从事一个涉及Winsock的C ++项目,但是对于复制构造函数,我对使用事件对象HANDLE类型的正确方法感到困惑。
概述(下面的代码):在尝试使用IOCP并使所有内容保持可伸缩性时,我有一个线程检查多个接受事件。每个ServerConnection对象都拥有由WSACreateEvent()创建的其自己的接受事件对象,其关联的低级套接字以及相关的状态/变量。
我的问题是,我正在尝试实施“三巨头”,但我不确定如何“复制”手柄。
DuplicateHandle()似乎创建了一个新的句柄,但它指向同一个对象,但是就ServerConnection“副本”而言,这是没有意义的(我们想要一个具有相同状态的新对象,对吗?)。
至于使用复制分配运算符,我不确定它将对事件对象HANDLEs做什么。
ServerConnection.h
class ServerConnection
{
public:
//...constructors, destructors, etc...
virtual HANDLE getAcceptEvent();
virtual void setAcceptEvent(HANDLE eventObj);
protected:
private:
HANDLE assocAcceptEvent;
//..other variables...
};
Run Code Online (Sandbox Code Playgroud)
ServerConnection.cpp
ServerConnection::ServerConnection(ServerConnection &that)
{
//blah blah...other vars
//? This does not seem right as the HANDLE is logically a pointer;
//Assigning like this just points another handle to the same event obj
//If the other ServerConnection object closes the handle...not good.
this.assocAcceptEvent = that.assocAcceptEvent;
//The only thing that make slightly …Run Code Online (Sandbox Code Playgroud) 我正在使用Visual Studio for C++,我们正在编写第一个代码,但是我遇到了一个"简单"的问题.
在代码中,我将每个部分作为一个函数本身使用,因此对于输出屏幕,它显示"命中输入",它调用一个函数在屏幕上显示.然而,在告别中,我更改了系统颜色,使背景为白色,文本为黑色,但仍需要显示"命中输入"功能.它确实如此,但由于它使用了它自己的颜色,现在有一条颜色,其中"\ t"在cout中.
如何在不会这样做的地方得到它?
#include <iostream> //Necessary for input/output
#include <string> //Necessary for constants
#include <Windows.h> //Necessary for colored text
using namespace std;
Run Code Online (Sandbox Code Playgroud)
呃......我以前从未这样做过......但我会单独发布这些部分.
system("cls");
system("color F0");
cout << "\n\n\n\n\n\n\n\t\tIt was a pleasure spending time with you, "
"User"
"!\n\n\n";
cout << "\t\t\t\350";
for (int i = 0; i < 31; i++){ cout << "\360"; }
cout << "\350\n";
cout << "\t\t\t\272";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15); //light grey
cout << " \311\315\273\332\304\277\332\304\277\332\302\277\332\277 \302 \302\332\304\277 ";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 240); //Black text …Run Code Online (Sandbox Code Playgroud) 我正在写一个反作弊的Win32加载器,在其中我需要创建一个事件,等待它通过我存储它的另一个进程发出信号 - 但是它失败了ERROR_INVALID_HANDLE.我正在创建一个未命名的事件,只是将它的HANDLE值传递给另一个进程,该进程应该在某种情况下将其设置为信号状态.任何想法为什么 - 这不是'CreateEvent'创建的HANDLE对所有过程都有效.伪代码说明了这一点:
Process1,Thread1:
extern LPVOID pExternalMemory;
extern HANDLE hExternalProcess; //Process2 Handle
extern HANDLE hExternalThread; //In suspended state (Thread1)
extern HANDLE hEventDuplicate;
HANDLE hEvent = CreateEvent(nullptr, true, false, nullptr);
DuplicateHandle(GetCurrentProcess(), hEvent, hExternalProcess, &hEventDuplicate, STANDARD_RIGHTS_ALL, false, 0); //Wrong, check EDIT1
WriteProcessMemory(hProcess, pExternalMemory, &hEventDuplicate, sizeof(HANDLE), nullptr);
ResumeThread(hExternalThread);
WaitForSingleObject(hEvent, INFINITE);
Run Code Online (Sandbox Code Playgroud)
Process2,Thread1:
EIP->
if(SomeCondition) SetEvent((HANDLE)ExternalMemory); //fails with 'ERROR_INVALID_HANDLE'
//Other code
Run Code Online (Sandbox Code Playgroud)
编辑:我使用'DuplicateHandle'来解决问题,但现在'第二个进程'SetEvent'调用失败,出现'ERROR_ACCESS_DENIED'.
编辑1:解决了问题 - 它是'DuplicateHandle'函数调用,它应该是
DuplicateHandle(GetCurrentProcess(), hEvent, hExternalProcess, &hEventDuplicate, 0, false, DUPLICATE_SAME_ACCESS)
Run Code Online (Sandbox Code Playgroud)
出于某种奇怪的原因 - 任何人都可以解释为什么这样?
如果我在名为 的批处理文件中执行以下操作test:
echo number=0 > file
Run Code Online (Sandbox Code Playgroud)
并运行它:
> test
> echo number=0 1> file
Run Code Online (Sandbox Code Playgroud)
file 包含:
number=0?
Run Code Online (Sandbox Code Playgroud)
如果我test改为:
echo number=1> file
Run Code Online (Sandbox Code Playgroud)
并运行它:
> test
> echo number= 1>file
Run Code Online (Sandbox Code Playgroud)
file 包含:
number=
Run Code Online (Sandbox Code Playgroud)
该1显然被解释为stdout 把手,这是可以理解的。
如果我test改为:
echo number=0> file
Run Code Online (Sandbox Code Playgroud)
-和2以9分别-并运行它:
> test
> echo number= 0>file
number=
Run Code Online (Sandbox Code Playgroud)
the0和2to9显然被解释为非stdout 句柄并且file是空的,这也是可以理解的。
和:
echo "number=1"> file
Run Code Online (Sandbox Code Playgroud)
file 包含:
"number=1"
Run Code Online (Sandbox Code Playgroud)
和:
echo …Run Code Online (Sandbox Code Playgroud) 想知道 Perl 中的句柄是什么。
我可以看到文件句柄、目录句柄等,但想知道 Perl 中句柄的含义。
例如,在 IO::Pipe 中,我可以看到下面的解释。并想弄清楚“成为把手”的含义?
reader ([ARGS])
The object is re-blessed into a sub-class of IO::Handle,
and becomes a handle at the reading end of the pipe. If
ARGS are given then fork is called and ARGS are passed
to exec.
Run Code Online (Sandbox Code Playgroud)
另外可以解释一下bless的意思吗?
我用来创建一个自定义函数,如winexec(...):Hwnd将重新执行已执行应用程序的句柄.
我确实使用了findwindow()但是如果它改变了窗口标题就有问题.