Thread: Heap Corruption when passing a vector of LPCWSTR's to a DLL.

  1. #1
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476

    Heap Corruption when passing a vector of LPCWSTR's to a DLL.

    Hi, sorry to trouble you again guys. Ok, this one is not a false alarm, I think.

    The problem occurs when I want to pass a vector of LPCWSTR's (null-terminated wide strings) to a DLL. After I finished using the vector, it would crash with ........ error dialog:

    This may be due to a corruption of the heap, which indicates a bug in photobooth.exe or any of the DLLs it has loaded.
    the code:
    Code:
    std::vector<LPCWSTR> m_vPlaylist; //BAWE-20100301: NEW_VIDEOPLAYER. Used in to VideoPlayer (attractmovie.dll) .
    Code:
    bool InitLayer::createAttractWindow(
    	SystemAccess* pSystemAccess,
    	HMONITOR hMonitor
    ) {
    	...
    	// AttractWindow is a class from another DLL so in this case I passed the vector of LPCWSTR to another dll.
    
    	m_pAttractWindow = AttractWindow::createInstance(
    		hInstance,
    		hMonitor,
    		m_vPlaylist //BAWE-20100301: NEW_VIDEOPLAYER. Crash!!!
    	);
    	...
    }
    Code:
    AttractWindow* AttractWindow::createInstance( HINSTANCE hInstance, HMONITOR hMonitor, std::vector<LPCWSTR> vPlaylist )
    {
    	...
    
    	m_pVideoPlayer = new VideoPlayer( hWnd, true );
    
    	m_pAttractWindow = new AttractWindow(
    		hInstance,
    		hWnd,
    		pVideoPlayer
    	);
    
    	m_pAttractWindow->init( vPlaylist );
    	...
    
    	return m_pAttractWindow;
    }
    
    void AttractWindow::init(std::vector<LPCWSTR> vPlaylist) { 
    
    	std::vector<LPCWSTR>::iterator iterPlaylist = vPlaylist.begin();
    
    
    	while (iterPlaylist != vPlaylist.end() )
    	{
    		m_pVideoPlayer->AddMovie( *iterPlaylist ); 
    		++ iterPlaylist;
    	}
    }
    I've stepped through "AttractWindow::init" and it worked fine. But after I get to the last line of "AttractWindow::createInstance" (the "return m_pAttractWindow" line), it crashed. Why did this happen? Thanks in advance.
    Last edited by g4j31a5; 03-02-2010 at 12:22 AM.
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    vector + DLL boundary = bad.

    Best practice is to not pass anything across a DLL boundary that is not compatible with C.
    You can pass the address of element zero of a vector to a DLL as an alternative when you just need to make the data visible inside the DLL. This isn't enough in your case though. You'll likely have to perform the memory allocation manually inside the DLL and not use a vector.

    In any case, you can not pass a vector by reference to anything where you wish to modify it. That's what references are for. Again though, references do not exist in C, so they are also something that by best practices should not be used across DLL boundaries.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Well, I don't intend to change the values of the vector but I need to pass a set of values to the DLL. So as far as I concerned, it can be a "const std::vector<LPCWSTR> &" if needed. Any other solution?

    EDIT: LoL!!! As a matter of fact using "const std::vector<LPCWSTR> &" works. The crash is gone now. Why didn't I think of that in the first place? Thanks for the hint iMalc.
    Last edited by g4j31a5; 03-02-2010 at 01:42 AM.
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

  4. #4

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Heap corruption detected. What does it mean?
    By franziss in forum C++ Programming
    Replies: 17
    Last Post: 07-23-2008, 02:50 AM
  2. Heap corruption errors
    By VirtualAce in forum C++ Programming
    Replies: 0
    Last Post: 07-15-2006, 04:46 PM
  3. Passing parameters from VB to C++ through ActiveX DLL
    By torbjorn in forum Windows Programming
    Replies: 0
    Last Post: 12-10-2002, 03:13 AM
  4. heap question
    By mackol in forum C Programming
    Replies: 1
    Last Post: 11-30-2002, 05:03 AM
  5. DLL & free() Heap Access Violation
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 08-13-2002, 10:45 PM