C++ 头文件中断一个程序,但在另一个程序中工作

C++ 头文件中断一个程序,但在另一个程序中工作,c++,header-files,msvc12,visual-c++-runtime,C++,Header Files,Msvc12,Visual C++ Runtime,我有下面的头文件- #pragma once #include <vector> template<int rows, int cols = rows> class mat { public: mat() : values{ rows, std::vector<double>(cols) } { if (rows == cols) { int y = 0; for (int x = 0;

我有下面的头文件-

#pragma once
#include <vector>

template<int rows, int cols = rows>
class mat {
public:
    mat() : values{ rows, std::vector<double>(cols) } {
        if (rows == cols) {
            int y = 0;
            for (int x = 0; x < rows; x++) {
                values[x][y] = 1;
                y++;
            }
        }
    }
    mat(int val) : values{ rows, std::vector<double>(cols, val) } {}
    std::vector<double> &operator[](int x) {
        return values[x];
    }
private:
    std::vector<std::vector<double>> values;
};
这是我引用的main.cpp文件-

mat<5> mat5;

for (int y = 0; y < 5; y++) {
    for (int x = 0; x < 5; x++) {
        std::cout << mat5[x][y] << ", ";
    }
    std::cout << std::endl;
}

解决办法相当简单。我之所以使用静态C运行库,是因为我认为在静态链接SDL2时需要这样做。事实上,您需要将/MD用于多线程DLL,并始终与SDL2一起使用。

Smth尝试释放以前未在此地址分配的内存。某些全局对象可能会破坏您的程序,而您尚未发布。我的系统中没有全局对象code@S.M.我的意思是,如果你想让我发布所有的代码,我不认为这是必要的,但我发布了github,以便有人可以在需要时查看整个代码。问题是,没有任何东西被释放。我可能很蠢,但到目前为止,代码中没有其他内容。它天生就是在调试器中运行的。好吧,我看了一下你的github。SDL2手册:将C运行时设置为“多线程DLL”。您的cmake:
set(cmake\u CXX\u FLAGS\u DEBUG)/MTd“
set(cmake\u CXX\u FLAGS\u RELEASE)/MT”)
mat<5> mat5;

for (int y = 0; y < 5; y++) {
    for (int x = 0; x < 5; x++) {
        std::cout << mat5[x][y] << ", ";
    }
    std::cout << std::endl;
}
>   ntdll.dll!RtlpBreakPointHeap()  Unknown
    ntdll.dll!RtlpValidateHeapEntry()   Unknown
    ntdll.dll!RtlValidateHeap() Unknown
    KernelBase.dll!HeapValidate()   Unknown
    physics.exe!_CrtIsValidHeapPointer(const void * block) Line 1407    C++
    physics.exe!free_dbg_nolock(void * const block, const int block_use) Line 904   C++
    physics.exe!_free_dbg(void * block, int block_use) Line 1030    C++
    physics.exe!__acrt_free_locale(__crt_locale_data * ptloci) Line 183 C++
    physics.exe!_updatetlocinfoEx_nolock(__crt_locale_data * * pptlocid, __crt_locale_data * ptlocis) Line 240  C++
    physics.exe!_wsetlocale::__l2::void <lambda>(void)::__l5::<lambda>() Line 518   C++
    physics.exe!__crt_seh_guarded_call<void>::operator()<void <lambda>(void),void <lambda>(void) &,void <lambda>(void)>(__acrt_lock_and_call::__l2::void <lambda>(void) && setup, _wsetlocale::__l2::void <lambda>(void)::__l5::void <lambda>(void) & action, __acrt_lock_and_call::__l2::void <lambda>(void) && cleanup) Line 224  C++
    physics.exe!__acrt_lock_and_call<void <lambda>(void)>(const __acrt_lock_id lock_id, _wsetlocale::__l2::void <lambda>(void)::__l5::void <lambda>(void) && action) Line 980   C++
    physics.exe!_wsetlocale::__l2::<lambda>() Line 528  C++
    physics.exe!__crt_seh_guarded_call<void>::operator()<void <lambda>(void),void <lambda>(void) &,void <lambda>(void) &>(__crt_call_and_cleanup::__l2::void <lambda>(void) && setup, _wsetlocale::__l2::void <lambda>(void) & action, _wsetlocale::__l2::void <lambda>(void) & cleanup) Line 224   C++
    physics.exe!__crt_call_and_cleanup<void <lambda>(void),void <lambda>(void)>(_wsetlocale::__l2::void <lambda>(void) && action, _wsetlocale::__l2::void <lambda>(void) && cleanup) Line 238   C++
    physics.exe!_wsetlocale(int _category, const wchar_t * _wlocale) Line 531   C++
    [Inline Frame] physics.exe!call_wsetlocale(const int category, const char * const narrow_locale) Line 30    C++
    physics.exe!setlocale::__l2::<lambda>() Line 41 C++
    physics.exe!__crt_seh_guarded_call<char *>::operator()<void <lambda>(void),char * <lambda>(void) &,void <lambda>(void)>(__acrt_lock_and_call::__l2::void <lambda>(void) && setup, setlocale::__l2::char * <lambda>(void) & action, __acrt_lock_and_call::__l2::void <lambda>(void) && cleanup) Line 204 C++
    [Inline Frame] physics.exe!__acrt_lock_and_call(const __acrt_lock_id) Line 976  C++
    physics.exe!setlocale(int _category, const char * _locale) Line 118 C++
    [External Code] 
    physics.exe!_initterm(void(*)() * first, void(*)() * last) Line 22  C++