传回从向量中检索的指针的问题

传回从向量中检索的指针的问题

本文介绍了传回从向量中检索的指针的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!在经历了13年失去我真正的编程技巧后,我决定重新回到C ++。我写了一个小程序,从网络摄像头读取并显示输出,现在我要回去尝试从这个应用程序中创建一个框架。我有一种方法不能按我的预期工作,我相信这里的专家可以提供帮助。



这就是我所拥有的。此方法询问设备驱动程序以提取有关设备的信息,并构建存储此信息的对象,然后将该对象放在向量中。这似乎工作正常。



 HRESULT WebCamDeviceList :: RegisterDevice(UINT32索引,IMFActivate * vDevice,IMFMediaSource * vMediaSource,IMFSourceReader * vMediaReader){ 
HRESULT hr = S_OK;
LPWSTR dFriendlyName = NULL;
UINT32 dName = NULL;
// dFriendlyName返回已分配的内存,需要清理为
//点
hr = vDevice [index] .GetAllocatedString(MF_DEVSOURCE_ATTRIBUTE_FRIENDLY_NAME,& dFriendlyName,& dName);

if(SUCCEEDED(hr)){
WebCamDevice * wDevice = new WebCamDevice();

wDevice-> SetIndex(index);
wDevice-> SetName(dName);
wDevice-> SetFriendlyName(dFriendlyName);

wDevice-> LoadConfiguration(vMediaSource,vMediaReader);

_DeviceList.push_back(wDevice);

} else {
MessageBox(NULL,TEXT(无法读取设备名称),TEXT(错误!),MB_ICONEXCLAMATION | MB_OK);
}


return hr;
}





这段代码从向量中检索该对象指针并将其传递回调用者。使用调试器检索对象指针似乎工作正常。问题是device参数被传回,没有任何值,就像它超出范围一样,如果是Java,我会认为GCer得到它。





 HRESULT WebCamDeviceList :: GetDevice(size_t index,WebCamDevice ** device){
HRESULT hr = S_OK;
WebCamDevice * cDevice = NULL;
if(index< _DeviceList.size()){
cDevice = _DeviceList [index];
}
device =& cDevice;
返回小时;
}







如果这有帮助,请拨打电话。行deviceList-> GetDevice(index,& device)传回一个空对象,即使它在退出之前就可以看到指针对调试器是有效的。



 WebCamDeviceList * deviceList = new WebCamDeviceList(); 
deviceList-> LoadDevices();
deviceList-> GetCount(& numberDevices);

size_t index = 0;

mainWindow =新的GuiMain(hWnd,hInstance);
while(index< numberDevices){
// MessageBox(NULL,TEXT(Device One),TEXT(Error!),MB_ICONEXCLAMATION | MB_OK);
WebCamDevice * device = NULL;
deviceList-> GetDevice(index,& device);
//((WebCamVideoConfig *)(device-> VideoConfig) - > WebCamVideoConfig :: SetWidth(width);
if(device-> GetFriendlyName()!= NULL){
SendMessage(mainWindow-> DeviceListBox,LB_ADDSTRING,0,(LPARAM)device-> GetFriendlyName());
}
index ++;
}





这必须是非常简单的事情我觉得傻瓜不理解发生了什么,因为我以前这样做是为了谋生但是Java已经毁了我的指针和记忆管理技巧。



提前感谢您的帮助。

Kevin

解决方案



Hi guys! After 13 years of losing my real programming skills by getting sucked into Java I've decided to dive back into C++. I wrote a small program to read from a webcam and display the output and now I'm going back and trying to make a framework out of this application. I have one method that just is not working how I would expect and I'm sure the experts here can help.

Here is what I have. This method interrogates the device driver to extract information about a device and builds an object that stores this information then places that object in a vector. This seems to work fine.

HRESULT WebCamDeviceList::RegisterDevice(UINT32 index, IMFActivate* vDevice,  IMFMediaSource* vMediaSource, IMFSourceReader* vMediaReader) {
	HRESULT hr = S_OK;
	LPWSTR dFriendlyName = NULL;
	UINT32 dName = NULL;
         //dFriendlyName returns allocated memory and will need to be cleaned up as some
         //point
	hr = vDevice[index].GetAllocatedString(MF_DEVSOURCE_ATTRIBUTE_FRIENDLY_NAME, &dFriendlyName, &dName);

	if(SUCCEEDED(hr)) {
		WebCamDevice* wDevice = new WebCamDevice();

		wDevice->SetIndex(index);
		wDevice->SetName(dName);
		wDevice->SetFriendlyName(dFriendlyName);

		wDevice->LoadConfiguration(vMediaSource, vMediaReader);

		_DeviceList.push_back(wDevice);

	} else {
		MessageBox(NULL,TEXT("Unable to retieve device name"), TEXT("Error!"), MB_ICONEXCLAMATION | MB_OK);
	}


	return hr;
}



This piece of code retrieves that object pointer from the vector and passes it back to the caller. Using the debugger the retrieval of the object pointer seems to work fine. The problem is that the "device" parameter is passed back with no values almost like it went out of scope and if it was Java I would assume the GCer got it.


HRESULT WebCamDeviceList::GetDevice(size_t index, WebCamDevice** device) {
	HRESULT hr = S_OK;
	WebCamDevice* cDevice = NULL;
	if (index < _DeviceList.size()) {
	  cDevice = _DeviceList[index];
	}
	device = &cDevice;
	return hr;
}




If this helps here is the caller. The line "deviceList->GetDevice(index, &device)" passes back a empty object even though right before it exits I can see the pointer is valid with the debugger.

WebCamDeviceList* deviceList = new WebCamDeviceList();
deviceList->LoadDevices();
deviceList->GetCount(&numberDevices);

size_t index = 0;

mainWindow = new GuiMain(hWnd,hInstance);
while( index < numberDevices ) {
    //MessageBox(NULL,TEXT("Device One" ), TEXT("Error!"), MB_ICONEXCLAMATION | MB_OK);
     WebCamDevice* device = NULL;
     deviceList->GetDevice(index, &device);
    //((WebCamVideoConfig*)(device->VideoConfig)->WebCamVideoConfig::SetWidth(width);
     if(device->GetFriendlyName() != NULL) {
         SendMessage(mainWindow->DeviceListBox, LB_ADDSTRING, 0, (LPARAM)device->GetFriendlyName());
     }
    index++;
}



This has to be something very simple and I feel like a fool not understanding what is going on since I used to do this for a living but Java has destroyed my pointer and memory management skills.

Thank you in advance for any help.
Kevin

解决方案



这篇关于传回从向量中检索的指针的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 14:44