473,888 Members | 1,419 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Release only problem calling a delegate from unmanaged code

I have a managed wrapper class that has a delegate to be called from
unmanaged code:

public ref class CmmMessageCallb ackWrapper
{
private:
delegate void InternalMessage Delegate( void*,
CmmService::Mes sage::CMessage* );
public:
CmmMessageCallb ackWrapper()
{
_InternalMessag eCallback = gcnew
InternalMessage Delegate(this,& CmmMessageCallb ackWrapper::Int ernalMessageCal lback);
}
void* GetPtrCallback( )
{
return
(void*)System:: Runtime::Intero pServices::Mars hal::GetFunctio nPointerForDele gate(_InternalM essageCallback) ;
}
private:
void InternalMessage Callback(void* pData,
CmmService::Mes sage::CMessage* pMessageData)
{
return;
}
InternalMessage Delegate^ _InternalMessag eCallback;
};
I then setup my unmanaged callback as follows:

bool CMyManagedClass ::CallUnmanaged ()
{
CmmMessageCallb ackWrapper^ Wrapper = gcnew
CmmMessageCallb ackWrapper();
FN_MESSAGE_CALL BACK pfnCallback = (FN_MESSAGE_CAL LBACK)Wrapper-
>GetPtrCallback ();
return _pUnmanagedObj->CallFuncWithCa llback(pfnCallb ack);
}

GetPtrCallback( ) returns the marshalled function pointer. Problem is
in a release build the callback causes a crash, in debug it is fine.

The only thing I can think of is the 'Wrapper' is garbage collected
before the delegate is called.
Help and advice greatfully received. I'm stuck.
Nov 21 '07 #1
3 1557
On 21 Nov, 16:03, ropo <ARop...@yahoo. co.ukwrote:
I have a managed wrapper class that has a delegate to be called from
unmanaged code:

public ref class CmmMessageCallb ackWrapper
{
private:
delegate void InternalMessage Delegate( void*,
CmmService::Mes sage::CMessage* );
public:
CmmMessageCallb ackWrapper()
{
_InternalMessag eCallback = gcnew
InternalMessage Delegate(this,& CmmMessageCallb ackWrapper::Int ernalMessageCal-lback);
}
void* GetPtrCallback( )
{
return
(void*)System:: Runtime::Intero pServices::Mars hal::GetFunctio nPointerForDele-gate(_InternalM essageCallback) ;
}
private:
void InternalMessage Callback(void* pData,
CmmService::Mes sage::CMessage* pMessageData)
{
return;
}
InternalMessage Delegate^ _InternalMessag eCallback;

};

I then setup my unmanaged callback as follows:

bool CMyManagedClass ::CallUnmanaged ()
{
CmmMessageCallb ackWrapper^ Wrapper = gcnew
CmmMessageCallb ackWrapper();
FN_MESSAGE_CALL BACK pfnCallback = (FN_MESSAGE_CAL LBACK)Wrapper->GetPtrCallback ();

return _pUnmanagedObj->CallFuncWithCa llback(pfnCallb ack);

}

GetPtrCallback( ) returns the marshalled function pointer. Problem is
in a release build the callback causes a crash, in debug it is fine.

The only thing I can think of is the 'Wrapper' is garbage collected
before the delegate is called.

Help and advice greatfully received. I'm stuck.
I think it is a problem of Garbage collection, changing the code to
this:

bool CMyManagedClass ::CallUnmanaged ()
{
CmmMessageCallb ackWrapper^ Wrapper = gcnew
CmmMessageCallb ackWrapper();
FN_MESSAGE_CALL BACK pfnCallback =
(FN_MESSAGE_CAL LBACK)Wrapper->GetPtrCallback ();

bool bRes = _pUnmanagedObj-
>CallFuncWithCa llback(pfnCallb ack);
Wrapper->GetPtrCallback ();
return bRes;
}

So that Wrapper is references after the function call, fixes the
problem. Anyone know why?
Nov 21 '07 #2
>The only thing I can think of is the 'Wrapper' is garbage collected
>before the delegate is called.
If that's the problem, you can add GC::KeepAlive(W rapper) to the end
of your method to prevent the object from being collected too soon.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 21 '07 #3
On 21 Nov, 19:08, Mattias Sjögren <mattias.dont.w ant.s...@mvps.o rg>
wrote:
The only thing I can think of is the 'Wrapper' is garbage collected
before the delegate is called.

If that's the problem, you can add GC::KeepAlive(W rapper) to the end
of your method to prevent the object from being collected too soon.

Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.orghttp://www.msjogren.ne t/dotnet/|http://www.dotnetinterop.com
Please reply only to the newsgroup.
Brilliant, Thanks that is exactly what I needed
Nov 22 '07 #4

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

Similar topics

0
1236
by: Bryan | last post by:
I have a multi-threaded C# console application that uses WMI (System.Management namespace) to make RPC calls to several servers (600+ ) and returns ScheduledJobs. The section of my code that performs the query is contained in a delegate function that I execute via a second thread. On 1 or 2 of the 600+ servers the query hangs. I've tried to use Thread.Join() coupled with a Thread.Abort() but this does not kill the thread. Based on...
15
11800
by: Bryan | last post by:
I have a multi-threaded C# console application that uses WMI (System.Management namespace) to make RPC calls to several servers (600+ ) and returns ScheduledJobs. The section of my code that performs the query is contained in a delegate function that I execute via a second thread. On 1 or 2 of the 600+ servers the query hangs. I've tried to use Thread.Join() coupled with a Thread.Abort() but this does not kill the thread. Based on...
4
4801
by: apm | last post by:
Is there a way to change the calling convention of a delegate to Cdecl? Is it possible to define a callback function in C# that used the Cdecl convention? Thanks in advance.
12
6127
by: Vasco Lohrenscheit | last post by:
Hi, I have a Problem with unmanaged exception. In the debug build it works fine to catch unmanaged c++ exceptions from other dlls with //managed code: try { //the form loads unmanaged dlls out of which unmanaged exception //get thrown
1
2606
by: H.B. | last post by:
Hi, I need to make a function that can display data on my Managed C++ app and be called by an unmanaged C++ DLL. Something like : void Form1::Form1_Load(System::Object * sender, System::EventArgs * e) { MyDLLInit(MyAppDisplayFunction); }
2
7321
by: zamir.khan | last post by:
Hello all, New to the groups, sorry if this the wrong forum/etiquette. I am coding a c++ application that requires the use of a timer-triggered event handler. I decided to use the timer provided in System::Timers::Timer. My understanding of the next part is not very good, as my code may reveal, but as I understand it, my application is "unmanaged C++", whereas the timer extension from the system DLL is managed. Therefore I needed to use...
0
2024
by: Haxan | last post by:
Hi, I have an unmanaged application that converts a function pointer to a delegate and then pass this as a parameter(delegate) to a managed function which then invokes it. Currently Im able to jump to this unmanaged function, but the values of the parameters inside this function Im seeing are not correct(they have some garbage values). //unmanaged class (C++ application)
0
1443
by: Anton | last post by:
I have a c# application, which calls a dll (which I made, so I can modify the source code), passes there several parameters. The Dll contains a thread pool. In the entry function which is called from c# a new job is assigned to the thread pool. As soon as job is completed in a separate thread, is calls back (from the thread) to the C# application. The problem is that sometimes (not at each time when the dll is called) I'm getting a...
6
3275
by: per9000 | last post by:
An interesting/annoying problem. I created a small example to provoke an exception I keep getting. Basically I have a C-struct (Container) with a function-pointer in it. I perform repeated calls to the function in the container. I allocate (and free in c) arrays to get garbage collection in both C and C#. After a few seconds I get an exception. I assume GarbageCollector moves the delegate (or collects is) and when I don't find it in C...
0
9961
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11181
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10886
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9597
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7990
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7148
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5819
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6014
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4245
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.