小编Jer*_*fin的帖子

内存泄漏:无法中断内存分配号

我正在尝试找到内存泄漏问题.

我的项目是一个基于ATL的对话框项目,它使用DirectShow和标准库.

我的程序总共有45个内存泄漏,每个都有24个字节.

我在stdafx.h中定义了#defn _CRTDBG_MAP_ALLOC等,以及DEBUG_NEW来获取每个内存泄漏的文件和行号.

但是,不会打印文件行号.内存块都是"普通"块,如下所示:

{180} normal block at 0x003E6008, 24 bytes long. Data: <  >  _>   > W   > A0 AE 3E 00 B0 5F 3E 00 A0 AE 3E 00 57 00 00 00 
Run Code Online (Sandbox Code Playgroud)

我已经尝试将以下行添加到_tWinMain()的开头

_CrtSetBreakAlloc(180);
Run Code Online (Sandbox Code Playgroud)

为了打破分配,但调试器根本没有中断.

任何人都可以告诉我如何追踪难以捉摸的内存泄漏?

最后,这是我的_tWinMain() - 我在退出之前调用_CrtDumpMemoryLeaks().

int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPTSTR lpstrCmdLine, int nCmdShow){
    _CrtSetBreakAlloc(180);

    HRESULT hRes = ::CoInitializeEx(NULL, COINIT_MULTITHREADED);
    ATLASSERT(SUCCEEDED(hRes));


    ::DefWindowProc(NULL, 0, 0, 0L);
    AtlInitCommonControls(ICC_BAR_CLASSES);

    //HINSTANCE hInstRich = ::LoadLibrary(CRichEditCtrl::GetLibraryName());

    hRes = _Module.Init(NULL, hInstance);
    ATLASSERT(SUCCEEDED(hRes));

    int nRet = Run(lpstrCmdLine, nCmdShow); …
Run Code Online (Sandbox Code Playgroud)

c++ memory-leaks

5
推荐指数
1
解决办法
3615
查看次数

如果变量属于堆或堆栈,我可以通过GDB找到吗?

我的断点指向一个变量.(指向某处.我可以弄清楚某处是属于堆还是堆栈?)

有没有办法找到堆栈的开始和结束的位置?有没有办法找到堆的开头和结尾?

c linux gdb

5
推荐指数
1
解决办法
4440
查看次数

有什么方法可以使用Qt而不为所有内容使用动态内存?

因此,我想我已经在网络上进行了彻底的搜索,发现没有什么真正有用的(最多只是令人困惑……)。

我想知道如何(如果可能)将Qt与非动态内存配合使用。我面临的问题是,对于许多小部件,我确切地知道我想要使用什么(这些子小部件,这些布局,固定数量等)。但是,当您不使用动态内存时,Qt中的所有内容似乎都会受到影响。一个简单的示例是QLayout,它来自Qt文档,旨在对所添加内容的所有权。所以基本上,下面的代码:

//In header
class ThumbnailDialog : public QDialog
{
    Q_OBJECT
public:
    ThumbnailDialog(QWidget* parent = 0);
    ~ThumbnailDialog(void);
private:
    QPushButton m_confirm;
    QPushButton m_cancel;

    QHBoxLayout m_buttonsLayout;
};

//Implementation of ctor
ThumbnailDialog::ThumbnailDialog(QWidget* parent):
    QDialog(parent)
{
     //...
     m_buttonsLayout.addWidget(&m_confirm);
     m_buttonsLayout.addWidget(&m_cancel);
     //...
     setLayout(&m_dialogLayout);
}
Run Code Online (Sandbox Code Playgroud)

...将最终(在MSVC上)导致调试断言失败,_BLOCK_TYPE_IS_VALID(pHead->nBlockUse)因为在ThumbnailDialogdtor中,布局试图删除按钮...显然不应该这样做。

因此,像“ Qt Expert”所提倡的那样,我是否被迫在所有地方都使用动态记忆(虽然提到了“堆”……)?这似乎是错误的,因为这阻止了RAII的使用(如果父子关系意味着将进行删除,那么我不能使用智能指针来执行此操作)。对于编译时已知的内容,诉诸动态内存也感觉非常错误……(但是我可能是错的,这只是我的感觉)。

所以:有没有一种方法可以使用Qt而不new为每个小部件/布局使用动态内存和s?

c++ qt dynamic-memory-allocation

5
推荐指数
1
解决办法
1147
查看次数

当非const成员可以在constexpr成员函数中使用?

我遇到了一个我不明白的情况.有人会这么好解释为什么第一个代码正确编译而第二个代码出错:

错误:'TestClass :: z'的值在常量表达式中不可用
static constexpr int sum(){return x + y + z;}
----------------- ----------------------------------- ^
注意:'int TestClass :: z'不是const static int Z者除外;"

工作代码:

#include <iostream>

using namespace std;

class TestClass
{
    public:
        constexpr int sum() {return x+y+z;}

    private:
        static constexpr int x = 2;
        static const int y = 3;
        int z = 5;

};

int main()
{
    TestClass tc;
    cout << tc.sum() << endl;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但是当我尝试制作TestClass::sum()静态时,我得到了上述错误:

#include <iostream>

using namespace std;

class TestClass
{ …
Run Code Online (Sandbox Code Playgroud)

c++ static class constexpr

5
推荐指数
1
解决办法
853
查看次数

在C++中将成员函数指针"强制转换"为函数指针的最简单方法是什么?

我想为STL算法的"comp"参数提供成员函数,如lower_bound(...,Compare comp).comp()函数访问非静态成员字段,因此它本身必须是非静态成员,但非静态成员函数指针的类型不同于普通函数指针的类型.

解决这个问题的最佳方法是什么?

c++ pointers function member

4
推荐指数
2
解决办法
567
查看次数

错误LNK2019:未解析的外部符号"public:

我在VS 2008中使用libtorrent创建了一个torrent应用程序.我尝试了链接中提供的示例http://www.rasterbar.com/products/libtorrent/examples.html BUt它显示了我这些错误....如何解决他们?

Linking...
main.obj : error LNK2019: unresolved external symbol "public: __thiscall libtorrent::torrent_info::~torrent_info(void)" (??1torrent_info@libtorrent@@QAE@XZ) referenced in function _main
main.obj : error LNK2019: unresolved external symbol "public: __thiscall libtorrent::torrent_info::torrent_info(struct libtorrent::lazy_entry const &)" (??0torrent_info@libtorrent@@QAE@ABUlazy_entry@1@@Z) referenced in function _main
main.obj : error LNK2019: unresolved external symbol "class std::basic_ostream > & __cdecl libtorrent::operator<<(class std::basic_ostream > &,struct libtorrent::lazy_entry const &)" (??6libtorrent@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV12@ABUlazy_entry@0@@Z) referenced in function _main
main.obj : error LNK2019: unresolved external symbol "int __cdecl libtorrent::lazy_bdecode(char const *,char const *,struct libtorrent::lazy_entry &,int)" (?lazy_bdecode@libtorrent@@YAHPBD0AAUlazy_entry@1@H@Z) referenced …
Run Code Online (Sandbox Code Playgroud)

c++ visual-studio-2008

4
推荐指数
1
解决办法
6万
查看次数

如果模板包含虚函数,是否安全?

模板的早期绑定和虚函数的后期绑定.因此,如果模板包含虚函数,是否安全?

template<typename T> 
class base {
public:
    T data;
    virtual void fn(T t){}
};
Run Code Online (Sandbox Code Playgroud)

c++ virtual

4
推荐指数
1
解决办法
676
查看次数

用C++进行预处理

如何在我同时定义两个指令时,我会得到编译错误?我想要错误

#define ENG
#define POL
#if defined POL
#if defined ENG
Run Code Online (Sandbox Code Playgroud)

这不是

#define ENG
//#define POL
#if defined POL
#if defined ENG
Run Code Online (Sandbox Code Playgroud)

preprocessor

4
推荐指数
1
解决办法
178
查看次数

使用MSYS在Windows 7中构建nginx

我有Windows 7 Pro x86和Visual Studio 2010 Pro.另外我在c:\ MinGW中有MinGW.我想使用Visual C++在windows下构建nginx.我按照指南.

  1. 我在管理员下运行cmd,然后我调用"c:\ Program Files\Microsoft Visual Studio 10.0\VC\vcvarsall.bat"
  2. 在cmd中我运行C:\ MinGW\msys\1.0\msys.bat
  3. 在我下载先决条件之前,我cd到nginx source direcotry并运行configure脚本.
  4. 然后我运行nmake -f objs/Makefile

结果是以下错误:

Microsoft (R) Program Maintenance Utility Version 10.00.40219.01
Copyright (C) Microsoft Corporation.  All rights reserved.

'install' is up-to-date
    cl -O2  -W4 -WX -nologo -MT -Zi -DFD_SETSIZE=1024 -DNO_SYS_TYPES_H 
       -Ycng x_config.h -Fpobjs/ngx_config.pch -c -I src/core-I src/event
       -I src/event/mod ules  -I src/os/win32  -I objs/lib/pcre-8.32  
       -I objs/lib/openssl/openssl/include -I objs/lib/zlib-1.2.7  -I objs
       -I src/http  -I src/http/modules -I src/mail -Foobjs/ngx_pch.obj 
       objs/ngx_pch.c …
Run Code Online (Sandbox Code Playgroud)

build nginx msys visual-c++

4
推荐指数
1
解决办法
2512
查看次数

foreach如何将对象转换为指定类型?

我对foreach行为有疑问C#.

我的自定义类实现了自定义GetEnumerator.此方法返回另一个object可隐式转换为的方法string.

但是,如果我foreach(string s in customClass)在运行时失败("无法将类型的对象转换为字符串").

但是,如果我这样做string x = new B()就像一个魅力.

注意:我没有必要在这里实现任何目标,我只是想了解发生了什么.我对这种通用行为特别感兴趣.

有任何想法吗?我错过了哪些基础知识?

代码复制这个:

public class A : IEnumerable
{
    #region IEnumerable Members

    public IEnumerator GetEnumerator()
    {
        yield return new B();
    }

    #endregion
}

public class B
{
    public static implicit operator string( B b )
    {
        return "to-stringed implicit";
    }
}

// CODE:

A a = new A();

// Works.
B b = …
Run Code Online (Sandbox Code Playgroud)

c# ienumerable foreach

4
推荐指数
2
解决办法
4590
查看次数