关于程序内存泄露问题?
程序可以运行,奇怪的是只进入主菜单界面后,就退出程序,并不会提示内存泄露;
但是当程序进入主菜单界面后,再进入实际游戏场景后,退出程序后就会提示内存泄露问题。
该问题的产生是我在添加DirectInput组件后产生的。
但是初始化输入设备是在进入主菜单界面之前做的,而非进入实际游戏场景之前做的。
并且如果我只进入主菜单界面,就算有按键行为,退出时依旧没有内存问题;
但是如果进入了实际游戏场景,就算没有按键行为,退出时依旧会提示内存问题;
而且每次进入实际游戏场景后再退出时,编译器都会给出如下信息:(相同的)
Direct3D9: (ERROR) :Memory still allocated! Alloc count = 3057
Direct3D9: (ERROR) :Current Process (pid) = 00000fb4(这个每次不一定相同)
Direct3D9: (ERROR) :Memory Address: 028e07fc lAllocID=1 dwSize=00004bc4, ReturnAddr=6480d5ea (pid=00000fb4)
Direct3D9: (ERROR) :Memory Address: 028e53f4 lAllocID=2 dwSize=00000350, ReturnAddr=648102fa (pid=00000fb4)
Direct3D9: (ERROR) :Memory Address: 028e577c lAllocID=3 dwSize=00000ef0, ReturnAddr=64816401 (pid=00000fb4)
Direct3D9: (ERROR) :Memory Address: 028f0064 lAllocID=5 dwSize=00006300, ReturnAddr=6481e508 (pid=00000fb4)
Direct3D9: (ERROR) :Memory Address: 028f639c lAllocID=6 dwSize=00001584, ReturnAddr=6480ec04 (pid=00000fb4)
Direct3D9: (ERROR) :Memory Address: 028f7954 lAllocID=7 dwSize=00000004, ReturnAddr=6480ed27 (pid=00000fb4)
Direct3D9: (ERROR) :Memory Address: 028e66a4 lAllocID=9 dwSize=000012fc, ReturnAddr=64803cab (pid=00000fb4)
.
.
.
.
.
Direct3D9: (ERROR) :Memory Address: 029dd4fc lAllocID=106 dwSize=0000002c, ReturnAddr=6480d5ea (pid=00000fb4)
Direct3D9: (ERROR) :Memory Address: 029dd55c lAllocID=107 dwSize=00000030, ReturnAddr=6480d5ea (pid=00000fb4)
Direct3D9: (ERROR) :Memory Address: 029dd5c4 lAllocID=108 dwSize=0000517c, ReturnAddr=6480d5ea (pid=00000fb4)
Direct3D9: (ERROR) :Memory Address: 048b0064 lAllocID=109 dwSize=00020020, ReturnAddr=6480d5ea (pid=00000fb4)
Direct3D9: (ERROR) :Total Memory Unfreed From Current Process = 3967089 bytes
请问如何解决这个问题?该如何调试?十分感谢了~~
[解决办法]
参考:
MSDN98\SAMPLES\VC98\SDK\GRAPHICS\DIRECTX\DIEX3\*.*
MSDN98\SAMPLES\VC98\SDK\GRAPHICS\DIRECTX\DIEX4\*.*
ftp.bartol.udel.edu/evenson/TRANSFER/DN60AENU1/SAMPLES/VC98/SDK/GRAPHICS/DIRECTX/DIEX3/
[解决办法]
记得用过后释放内存了没。。?使用new 创建后,要使用delete释放了。。
如果没有使用,则使包含指针的内存由于作用域和对象生命周期的原因而被释放,在自由存储空间上动态分配的变量或结构将继续存在。实际上,将会无法访问自由存储空间中的结构,因为指向这些内存的指针无效。这将导致内存泄露
[解决办法]
不能断点之后进行单步调试么?
或者看看调用堆栈?
[解决办法]
windbg看看heap吧!
[解决办法]
C:\samples\VC98\sdk\Graphics\DirectX\diex3\DIEX3.CPP节选:
.../**************************************************************************** * * DIInit * * Initialize the DirectInput variables. * * This entails the following four functions: * * DirectInputCreate * IDirectInput::CreateDevice * IDirectInputDevice::SetDataFormat * IDirectInputDevice::SetCooperativeLevel * ****************************************************************************/BOOLDIInit( HWND hwnd){ HRESULT hr; /* * Register with the DirectInput subsystem and get a pointer * to a IDirectInput interface we can use. * * Parameters: * * g_hinst * * Instance handle to our application or DLL. * * DIRECTINPUT_VERSION * * The version of DirectInput we were designed for. * We take the value from the <dinput.h> header file. * * &g_pdi * * Receives pointer to the IDirectInput interface * that was created. * * NULL * * We do not use OLE aggregation, so this parameter * must be NULL. * */ hr = DirectInputCreate(g_hinst, DIRECTINPUT_VERSION, &g_pdi, NULL); if (FAILED(hr)) { Complain(hwnd, hr, "DirectInputCreate"); return FALSE; } /* * Obtain an interface to the system keyboard device. * * Parameters: * * GUID_SysKeyboard * * The instance GUID for the device we wish to access. * GUID_SysKeyboard is a predefined instance GUID that * always refers to the system keyboard device. * * &g_pKeyboard * * Receives pointer to the IDirectInputDevice interface * that was created. * * NULL * * We do not use OLE aggregation, so this parameter * must be NULL. * */ hr = g_pdi->CreateDevice(GUID_SysKeyboard, &g_pKeyboard, NULL); if (FAILED(hr)) { Complain(hwnd, hr, "CreateDevice"); return FALSE; } /* * Set the data format to "keyboard format". * * A data format specifies which controls on a device we * are interested in, and how they should be reported. * * This tells DirectInput that we will be passing an array * of 256 bytes to IDirectInputDevice::GetDeviceState. * * Parameters: * * c_dfDIKeyboard * * Predefined data format which describes * an array of 256 bytes, one per scancode. */ hr = g_pKeyboard->SetDataFormat(&c_dfDIKeyboard); if (FAILED(hr)) { Complain(hwnd, hr, "SetDataFormat"); return FALSE; } /* * Set the cooperativity level to let DirectInput know how * this device should interact with the system and with other * DirectInput applications. * * Parameters: * * DISCL_NONEXCLUSIVE * * Retrieve keyboard data when acquired, not interfering * with any other applications which are reading keyboard * data. * * DISCL_FOREGROUND * * If the user switches away from our application, * automatically release the keyboard back to the system. * */ hr = g_pKeyboard->SetCooperativeLevel(hwnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND); if (FAILED(hr)) { Complain(hwnd, hr, "SetCooperativeLevel"); return FALSE; } return TRUE;}/**************************************************************************** * * DITerm * * Terminate our usage of DirectInput. * ****************************************************************************/voidDITerm(void){ /* * Destroy any lingering IDirectInputDevice object. */ if (g_pKeyboard) { /* * Cleanliness is next to godliness. Unacquire the device * one last time just in case we got really confused and tried * to exit while the device is still acquired. */ g_pKeyboard->Unacquire(); g_pKeyboard->Release(); g_pKeyboard = NULL; } /* * Destroy any lingering IDirectInput object. */ if (g_pdi) { g_pdi->Release(); g_pdi = NULL; }}...
[解决办法]
要是代码比较复杂的话,使用c/c++内存检测工具Leak Detector 来查吧!可以辅助你找到内存泄漏的地方