Mixed managed\unmanaged dll causing problems with std::vector clear()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hanthehead
    New Member
    • Oct 2007
    • 4

    Mixed managed\unmanaged dll causing problems with std::vector clear()

    In order to use a C# dll from our unmanaged C++ project I have created a mixed managed\unmanag ed c++ dll.
    This dll is passed an unmanaged class which contains vector<CDBRecor d> m_vecRecords member variable. Within the managed C++ dll m_vecRecords is added to. This all works fine until our unmanaged C++ project attempts to m_vecRecords.cl ear() and I get an exception.

    "Microsoft C++ exception: std::bad_alloc at memory location 0x0012ed68.."
    and execution stops at
    > msvcp90d.dll!st d::allocator<ch ar>::allocate(u nsigned int _Count=23152035 57) Line 151 + 0xb bytes C++
    Code:
    	void destroy(pointer _Ptr)
    		{	// destroy object at _Ptr
    		_Destroy(_Ptr);
    		}
    then
    Code:
    	pointer allocate(size_type _Count)
    		{	// allocate array of _Count elements
    		return (_Allocate(_Count, (pointer)0));
    		}
    Here are some exerts of my code....I hope you can help!
    Code:
    // IReadDatabase.h
    #pragma once
    #include "stdafx.h"
    #include <vector>
    
    #pragma unmanaged
    
    class StringPair
    {
    public:
    	char *key;	/* optional element of type xsd:string */
    	char *value;	/* optional element of type xsd:string */
    	StringPair();	/* transient */
    };
    
    class CDBRecord
    {
    public:
    	CDBRecord(long dbID = -1);
    	
    	StringPair *__ptr;
    	int __size;
    };
    
    class CHitlist
    {
    public:		
    	CHitlist() {}
    	
    	long				m_hit_total;
    	
    	std::vector<CDBRecord>   m_vecRecords;	
    };
    .....
    
    // ManagedDll.h
    
    #pragma once
    
    #include <IReadDatabase.h>
    
    #using <..\..\Shared Libraries\c#DLL.dll>
    using namespace System;
    using namespace System::Collections::Generic;
    using namespace c#DLL;
    
    #pragma managed
    
    namespace ManagedDll
    {		
    	bool CManagedDll::AddRecord(CHitlist* pHitlist)
    	{
    		CDBRecord rec;
    		bool bRet = GetRecord(&rec);
    		
    		if (bRet)
    			pHitlist->m_vecRecords.push_back(rec);
    
    		return bRet;
    	}
    ....
    Last edited by Frinavale; Sep 16 '08, 05:54 PM. Reason: added [code] tags
Working...