View Full Version : Different behavior of class libs from 1.41 to 2.0

February 28, 2005, 02:51:06

I'm in the process of updating to the 2.0 class library from the 1.41 version. I've come across some strange things and thought I'd check in the forum to see if anyone else has run into this.

In the 1.41 version of my code, I get the list of available video devices:

pVidCapDevList = m_pGrabber->getAvailableVideoCaptureDevices();

then iterate through the list:

for( DShowLib::Grabber::tVidCapDevListPtr::value_type:: iterator it = pVidCapDevList->begin();
it != pVidCapDevList->end();
it++ )
char *str = const_cast<char*> (( DShowLib::Grabber::tVideoCaptureDeviceItem) *it ).getName().c_str();

Note this last line, where I need to get the VCD name from the device driver as a char*. In the 1.41 class library, this worked as expected, with the str being "1394 Firewire...". However, with the 2.0 class library, the value is a long string of the same weird character - definitely not the correct VCD name. However, the .getUniqueName() method seems to work correctly.

This difference is not unique to this method; the getAvailableInputChannels() method has a similar difference.

Is there something simple that I am overlooking? Is this something that was changed in 2.1 version and I should get that instead?

thanks in advance...

Ron Cordell

March 1, 2005, 11:50:47

I think I know the souce of your problem.

char *str = const_cast<char*> (( DShowLib::Grabber::tVideoCaptureDeviceItem) *it ).getName().c_str();

With this line you do serveral things. Written clearly, this line would resolve to :

char* str;
DShowLib::Grabber::tVideoCaptureDeviceItem compilerTmp( *it );
std::string cmpTmpStr = compilerTmp.getName();
const char* tmpStr = cmpTmpStr.c_str();
str = const_cast<char*>( tmpStr );
// now str points to an temporary string, and so may point into garbage ...

The compiler may throw away these temporaries anytime, after the expression.

So better use something like :

std::string str = it->getName();
// here use str ...

So if you want to unpack the char array contained in the return value from getName(), you must copy it into your own. (CString from the MFC or the std::string returned, own their memory, so as long as you hold them, the string contained is held alive by them).

Best regards

Christopher Koschack