简体   繁体   English

编辑EDIT会导致程序崩溃

[英]Editing EDIT causes program crash

I've problem with Editing EDIT window (both by writing or sending text to it). 我在编辑EDIT窗口时遇到问题(通过向其写入或发送文本)。 Here's my code . 这是我的代码

Few days ago i had problem for which solution was deleting MSG handling loop from new windows. 几天前,我遇到了哪个解决方案正在从新窗口删除MSG处理循环的问题。 Everything would be fine, but it caused another problem. 一切都会好起来的,但是这引起了另一个问题。 I can't edit EDIT windows. 我无法编辑“编辑”窗口。 When that handling loop is in new window Editing works, when not it doesn't. 当该处理循环出现在新窗口中时,编辑起作用,否则,则不起作用。

Parent window - MainWindow 父窗口 - 主窗口
Child windows - ChatWindow 子窗口-ChatWindow

There's very few tutorials about Winapi, so sometimes i have to use code that i don't fully understand(like 2 Wndproc methods to handle window action's) 关于Winapi的教程很少,所以有时我不得不使用我不完全理解的代码(例如2个Wndproc方法来处理窗口动作)

Thank's for your time 谢谢你的时间

OK here's the problem, this 好,这是问题所在

ChatWindow::ChatWindow()
{
    ...
    window = CreateWindowEx( WS_EX_CLIENTEDGE, 
        NazwaKlasy,windowTitle,WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT,DEFAULT_WIDTH , 
        DEFAULT_HEIGHT, NULL, NULL, hIns, this );
    ...

and this 和这个

okna.push_back(ChatWindow(dwSel,(char*)chBuffer));

In the ChatWindow constructor you store the this pointer (see the last parameter to CreateWindowEx ). ChatWindow构造函数中,存储this指针(请参阅CreateWindowEx的最后一个参数)。 But when you save the ChatWindow object you save a copy in the okna vector. 但是,当您保存ChatWindow对象时,会在okna向量中保存一个副本 So the address of the ChatWindow object passed to CreateWindowEx is not the same as the address of the ChatWindow object in your vector. 所以地址ChatWindow传递给目标CreateWindowEx是不一样的地址ChatWindow在矢量对象。

Instead of std::vector<ChatWindow> okna; 代替std::vector<ChatWindow> okna; you should have a vector of pointers std::vector<ChatWindow*> okna; 你应该有一个指针向量std::vector<ChatWindow*> okna; . That should fix the problem. 那应该解决问题。

The basic issue with your design is that your window objects are not copyable because you are telling Windows what the this pointer is. 设计的基本问题是,窗口对象不可复制,因为您正在告诉Windows this指针是什么。 You should add a private copy constructor and assignment operator to stop you copying them by mistake, like this. 您应该添加一个私有副本构造函数和赋值运算符,以防止您错误地复制它们,就像这样。

class NewWindow
{
private:
  NewWindow(const NewWindow&); // prevent copying
  NewWindow& operator=(const NewWindow&); // prevent copying
};

Oh, and I second what Jerry says, get a copy of Petzold. 哦,我同意杰里所说的,得到了​​彼得佐尔德的副本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM