469,920 Members | 2,363 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,920 developers. It's quick & easy.

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\unmanaged c++ dll.
This dll is passed an unmanaged class which contains vector<CDBRecord> 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.clear() and I get an exception.

"Microsoft C++ exception: std::bad_alloc at memory location 0x0012ed68.."
and execution stops at
> msvcp90d.dll!std::allocator<char>::allocate(unsign ed int _Count=2315203557) Line 151 + 0xb bytes C++
Expand|Select|Wrap|Line Numbers
  1.     void destroy(pointer _Ptr)
  2.         {    // destroy object at _Ptr
  3.         _Destroy(_Ptr);
  4.         }
then
Expand|Select|Wrap|Line Numbers
  1.     pointer allocate(size_type _Count)
  2.         {    // allocate array of _Count elements
  3.         return (_Allocate(_Count, (pointer)0));
  4.         }
  5.  
Here are some exerts of my code....I hope you can help!
Expand|Select|Wrap|Line Numbers
  1. // IReadDatabase.h
  2. #pragma once
  3. #include "stdafx.h"
  4. #include <vector>
  5.  
  6. #pragma unmanaged
  7.  
  8. class StringPair
  9. {
  10. public:
  11.     char *key;    /* optional element of type xsd:string */
  12.     char *value;    /* optional element of type xsd:string */
  13.     StringPair();    /* transient */
  14. };
  15.  
  16. class CDBRecord
  17. {
  18. public:
  19.     CDBRecord(long dbID = -1);
  20.  
  21.     StringPair *__ptr;
  22.     int __size;
  23. };
  24.  
  25. class CHitlist
  26. {
  27. public:        
  28.     CHitlist() {}
  29.  
  30.     long                m_hit_total;
  31.  
  32.     std::vector<CDBRecord>   m_vecRecords;    
  33. };
  34. .....
  35.  
  36. // ManagedDll.h
  37.  
  38. #pragma once
  39.  
  40. #include <IReadDatabase.h>
  41.  
  42. #using <..\..\Shared Libraries\c#DLL.dll>
  43. using namespace System;
  44. using namespace System::Collections::Generic;
  45. using namespace c#DLL;
  46.  
  47. #pragma managed
  48.  
  49. namespace ManagedDll
  50. {        
  51.     bool CManagedDll::AddRecord(CHitlist* pHitlist)
  52.     {
  53.         CDBRecord rec;
  54.         bool bRet = GetRecord(&rec);
  55.  
  56.         if (bRet)
  57.             pHitlist->m_vecRecords.push_back(rec);
  58.  
  59.         return bRet;
  60.     }
....
Sep 16 '08 #1
0 1836

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

10 posts views Thread by Stefan Höhne | last post: by
5 posts views Thread by canned.net | last post: by
6 posts views Thread by lokchan | last post: by
19 posts views Thread by Daniel Pitts | last post: by
reply views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.