473,385 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

_CrtIsValidHeapPointer and templates

I have a native DLL which is called from a managed application. One of
the calls to the DLL returns a std::vector<Cell(by value) where
Cell is a POD type, everything works fine until the function making
the call to the DLL returns, at which point I get a
CrtIsValidHeapPointer assertion failure.

As I understand things this is because the Cell-objects in the
std::vector are allocated on the DLL's heap but when the function
returns in the managed application (and the automatic variable goes
out of scope) and the std::vector's destructor is called it is
"working" on the managed heap.

Is there a way to solve this without having to rewrite the DLL, I
would prefer not to return a pointer to the std::vector<Cellsince it
(currently) is created on the fly from a much larger, internal,
structure.

--
Erik Wikström

May 16 '07 #1
6 2215
>I have a native DLL which is called from a managed application. One of
>the calls to the DLL returns a std::vector<Cell(by value) where
Cell is a POD type, everything works fine until the function making
the call to the DLL returns, at which point I get a
CrtIsValidHeapPointer assertion failure.

As I understand things this is because the Cell-objects in the
std::vector are allocated on the DLL's heap but when the function
returns in the managed application (and the automatic variable goes
out of scope) and the std::vector's destructor is called it is
"working" on the managed heap.
Erik,

Ensure that the DLL and the EXE are both built with the same version
of VS and that they both use the DLL version of the 'C' run-time
library - that way they'll use a common native heap. Also - don't mix
debug/release modules.

Dave
May 16 '07 #2
(sorry for top-post, OE yet again fails to quote)
native types, such as std::vector, are never instantiated on the managed
heap.

When you say that Cell is a POD type, I assume it is declared as a native
struct in both the application and the DLL?

"Erik Wikström" <er****@student.chalmers.sewrote in message
news:11**********************@u30g2000hsc.googlegr oups.com...
I have a native DLL which is called from a managed application. One of
the calls to the DLL returns a std::vector<Cell(by value) where
Cell is a POD type, everything works fine until the function making
the call to the DLL returns, at which point I get a
CrtIsValidHeapPointer assertion failure.

As I understand things this is because the Cell-objects in the
std::vector are allocated on the DLL's heap but when the function
returns in the managed application (and the automatic variable goes
out of scope) and the std::vector's destructor is called it is
"working" on the managed heap.

Is there a way to solve this without having to rewrite the DLL, I
would prefer not to return a pointer to the std::vector<Cellsince it
(currently) is created on the fly from a much larger, internal,
structure.

--
Erik Wikström
May 17 '07 #3
On 17 Maj, 17:23, "Ben Voigt" <r...@nospam.nospamwrote:
"Erik Wikström" <eri...@student.chalmers.sewrote in message
>>
news:11**********************@u30g2000hsc.googleg roups.com...
I have a native DLL which is called from a managed application. One of
the calls to the DLL returns a std::vector<Cell(by value) where
Cell is a POD type, everything works fine until the function making
the call to the DLL returns, at which point I get a
CrtIsValidHeapPointer assertion failure.

As I understand things this is because the Cell-objects in the
std::vector are allocated on the DLL's heap but when the function
returns in the managed application (and the automatic variable goes
out of scope) and the std::vector's destructor is called it is
"working" on the managed heap.

Is there a way to solve this without having to rewrite the DLL, I
would prefer not to return a pointer to the std::vector<Cellsince it
(currently) is created on the fly from a much larger, internal,
structure.

(sorry for top-post, OE yet again fails to quote)
Fixed
native types, such as std::vector, are never instantiated on the managed
heap.

When you say that Cell is a POD type, I assume it is declared as a native
struct in both the application and the DLL?
Yes, it's declared in a headerfile in the DLL-project which is also
included in the EXE-project.

To David Lowndes:
They are both part of the same solution in Visual C++, and if that
does not give the right versions then I don't know how.

--
Erik Wikström

May 18 '07 #4
Erik Wikström wrote:
To David Lowndes:
They are both part of the same solution in Visual C++, and if that
does not give the right versions then I don't know how.
You set the CRT version in the project properties->C/C++->Code
Generation->Runtime Library. Both modules have to use a DLL version of
the CRT, and both have to use the same one, to ensure that they share
their heap.

Tom
May 18 '07 #5
On 18 Maj, 17:06, "Tom Widmer [VC++ MVP]" <tom_use...@hotmail.com>
wrote:
Erik Wikström wrote:
To David Lowndes:
They are both part of the same solution in Visual C++, and if that
does not give the right versions then I don't know how.

You set the CRT version in the project properties->C/C++->Code
Generation->Runtime Library. Both modules have to use a DLL version of
the CRT, and both have to use the same one, to ensure that they share
their heap.
Ah, that's the problem. For some reason I have set the DLL to be
statically linked to the CRT (don't know why) and I was sure I hadn't
changed that.

Thank you al for your help.

--
Erik Wikström

May 21 '07 #6
PLS
In article <11**********************@u30g2000hsc.googlegroups .com>,
er****@student.chalmers.se says...
I have a native DLL which is called from a managed application. One of
the calls to the DLL returns a std::vector<Cell(by value) where
Cell is a POD type, everything works fine until the function making
the call to the DLL returns, at which point I get a
CrtIsValidHeapPointer assertion failure.

As I understand things this is because the Cell-objects in the
std::vector are allocated on the DLL's heap but when the function
returns in the managed application (and the automatic variable goes
out of scope) and the std::vector's destructor is called it is
"working" on the managed heap.

Is there a way to solve this without having to rewrite the DLL, I
would prefer not to return a pointer to the std::vector<Cellsince it
(currently) is created on the fly from a much larger, internal,
structure.

--
Erik Wikström
This is a bit late, since you don't want to change the dll. But for what
it's worth, the other suggestions in this thread about matching runtimes
will work but I consider them risky and too easy to break with future
development.

My own approach is that whenever a dll has to allocate memory it gets
passed in an allocator function that allocates in the caller heap. I
will frequently use CoGetMalloc for this.

This way, no matter how the run times shake our, you still have
consistant allocation.

++PLS
May 24 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Tom Alsberg | last post by:
Hi there... I'm recently trying to get a bit acquainted with XML Schemas and XSL. Now, I have a few questions about XSL stylesheets and templates: * Is there a way to "enter" a child element...
22
by: E. Robert Tisdale | last post by:
According to the C++ FAQ Lite: http://www.parashift.com/ What is "genericity"? Yet another way to say, "class templates." Not to be confused with "generality" (which just means avoiding...
16
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
4
by: Adriano Coser | last post by:
I'm having a _CrtIsValidHeapPointer assertion at the _free_dbg_lk function. I'm calling a function from a DLL witch allocates (malloc) a char array and the exception occours freeing the array. ...
0
by: jiing | last post by:
When I execute my program (it's multithread and has COM and dll), there is an error message as follows: Program D:\dongle_1\TestAP\bin\Debug\TestAP.exe File: dbgheap.c Line: 1132 Expression:...
0
by: jiing | last post by:
When I execute my program (it's multithread and has COM and dll), there is an error message as follows: Program D:\dongle_1\TestAP\bin\Debug\TestAP.exe File: dbgheap.c Line: 1132 Expression:...
25
by: Ted | last post by:
I'm putting the posts that follow here (hopefully they will follow here!) because they were rejected in comp.lang.c++.moderated. It behooves anyone reading them to first read the the thread of the...
28
by: NewToCPP | last post by:
Hi, I am just trying to find out if there is any strong reason for not using Templates. When we use Templates it is going to replicate the code for different data types, thus increasing the...
13
by: Schizoid Man | last post by:
Hi, I have defined a very simple class as follows. I initialize the CInventory object as: CInventory *itemInveotry; The program just performs two functions - it reads the total number of...
3
by: =?Utf-8?B?SnNDaGFybHk=?= | last post by:
Hello, My application is using a Dll loaded with the function "LoadLibrary(<NameOfTheDll>)" In the dll there is the following fonction : char * GetMessageError(int Error) {
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.