Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


how to free allocated memory in C++ dll function which is called from C# module

Posted on 2015-01-12
Medium Priority
Last Modified: 2015-01-19
Dear All

Do we have any methodology to free allocated memory in C++ dll function which is called from C# module.
Its quite urgent.Can u please help.

Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +2
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40544323
Who allocates the memory? Is it the C++ or the C#?
LVL 86

Expert Comment

ID: 40544503
Furthermore, if it is done in the C++ DLL, that will also depend how the allocation was done,, i.e. via 'new', 'new[]', 'malloc()' or some OS-specific API. In other words: That can be quite a simple task if you have the source code and close to impossible if you haven't.

Author Comment

ID: 40546031
Thanks Sir for the reply,

Memory is getting allocated in C++ DLL only.
Actually This DLL cod eis very big and its an ATL COM DLL.Hence so many places BSTR,_bstr_t,and other form of strings are getting used.Moreover its data is very crucially calculated and tested too.Hence we dont want to change the DLL Code.Only when we hit the DLL number of times,then it crashes unexpectedly.

If we can do something from managed code level from C# code,that will be beneficial.

Thanks again.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 86

Expert Comment

ID: 40546595
No, there is hardly anything you could do from managed code. What exactly is the exception that causes the crash?
LVL 23

Assisted Solution

by:Michael Fowler
Michael Fowler earned 750 total points
ID: 40548105
You could look at disposing the COM object after use with Marshal.ReleaseComObject

This method is used to explicitly control the lifetime of a COM object used from managed code. You should use this method to free the underlying COM object that holds references to resources in a timely manner or when objects must be freed in a specific order.

Author Comment

ID: 40548230
@jkr, the popup just triggered at server that w3wp.exe stopped you want to debug.when we say ok then again service starts working atleast for some time.Earlier also it happened so many times.

@Michaels,actually I am already using it but I am not sure whether it is fulfilling the requirement or not.
LVL 23

Expert Comment

by:Michael Fowler
ID: 40548239
Another suggestion is that it well known that when using the Office Interop that you need to release all objects created under the initial com object. When using the office interop it is highly recommended to never use an expression with 2 dots but rather decalre objects for these objects eg

Something similar could be happening here
LVL 22

Accepted Solution

ambience earned 750 total points
ID: 40549297
Often there is no easy way to catch and fix such problems. There are a few things worth a try:

Setup Drwatson or enable coredump writing in Windows. This should give you something to analyze under WinDbg, which will at least tell about the nature of crash, exception codes etc.

As for memory issues, you might have to resort to profiling the application using 3rd party tools, open/free or paid.

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question