Solved

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

Posted on 2015-01-12
8
233 Views
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.

Thanks
0
Comment
Question by:BYTECHINDIA
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 74

Expert Comment

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

Expert Comment

by:jkr
Comment Utility
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.
0
 

Author Comment

by:BYTECHINDIA
Comment Utility
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.
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
No, there is hardly anything you could do from managed code. What exactly is the exception that causes the crash?
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 23

Assisted Solution

by:Michael74
Michael74 earned 250 total points
Comment Utility
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.
http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.releasecomobject.aspx
0
 

Author Comment

by:BYTECHINDIA
Comment Utility
@jkr, the popup just triggered at server that w3wp.exe stopped working.do 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.
0
 
LVL 23

Expert Comment

by:Michael74
Comment Utility
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

http://msdn.microsoft.com/en-us/library/bb623945(v=office.12).aspx

Something similar could be happening here
0
 
LVL 22

Accepted Solution

by:
ambience earned 250 total points
Comment Utility
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.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The purpose of this article is to demonstrate how we can use conditional statements using Python.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now