473,569 Members | 2,782 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do you prevent garbage collection?

Hi,

I think I'm having some problems here with garbage collection. Currently, I
have the following code:

public struct Event

{

public int timestamp;

public EventType event;

public short device;

public int status;

public int input;

}

and the following method using the above struct:

unsafe private void Message( int msg )

{

Event myEvent = new Event();

while ( GetEvent(myEven t) != -1 )

{

// do some work

}

}

GetEvent is a call to an external DLL written in C++. When I send the
myEvent reference to the DLL, I receive a 'System.NullRef erenceException '.
I believe this is because that myEvent gets garbage collected shortly after
the function call to the dll. Am I correct? How do I prevent the struct
(or reference to the struct) from being garbage collected until after the
function call is complete?

Thanks in advance,

Chris
Nov 13 '05 #1
4 12318
Hello Chirs,

I too do not think that, what you are seeing is because of Garbage
Collection.
The easiest way to check whether your Object has been removed or not is to
print values after the Call to DLL.

But, I think its due to the nature of the two different platforms(if I can
call .NET as platform).
When you create an object in .NET its part of the managed application. So I
do not think you can pass the reference to Unamanaged app to use that memory
address.
Firstly it may not be the true address and secondly the values stored may
not be similar to how values are stored in a plain C++/VB/Windows app. For
example in remoting the objects are transported by Boxing/Unboxing and
similar should happen in your case.

So you should be looking at "How to pass values between managed and
Unmanaged applications"

One somewhat relevant I could find quikcly is
http://msdn.microsoft.com/netframewo...allcomcomp.asp

I am not sure that the above can answer all your questions but I hope it
puts on the right path to solving the issue.

If you still think it is because of the GC the easy way to prevent GC is by
having a reference to that Object
and not release until you are done calling your external(unaman ged ) app.
Good luck
Sridhar

"Chris" <c w a n @ n o s p a m - v i g i l . c o m> wrote in message
news:u8******** ******@TK2MSFTN GP10.phx.gbl...
Hmm ... I'm not completely sure either. The interface signature in the DLL is as follows:
long GetEvent (Event* pEvent );

When I send 'myEvent' via my C# host application, an address of 0x00000000
is assigned to 'pEvent'.

Chris
"Tu-Thach" <tu*****@yahoo. com> wrote in message
news:01******** *************** *****@phx.gbl.. .
I don't think it is because of Garbage collection. You
might want to check out what GetEvent looks like.

Tu-Thach
-----Original Message-----
Hi,

I think I'm having some problems here with garbage

collection. Currently, I
have the following code:

public struct Event

{

public int timestamp;

public EventType event;

public short device;

public int status;

public int input;

}

and the following method using the above struct:

unsafe private void Message( int msg )

{

Event myEvent = new Event();

while ( GetEvent(myEven t) != -1 )

{

// do some work

}

}

GetEvent is a call to an external DLL written in C++.

When I send the
myEvent reference to the DLL, I receive

a 'System.NullRef erenceException '.
I believe this is because that myEvent gets garbage

collected shortly after
the function call to the dll. Am I correct? How do I

prevent the struct
(or reference to the struct) from being garbage collected

until after the
function call is complete?

Thanks in advance,

Chris
.


Nov 13 '05 #2
Hi Sridhar,

Thanks for your comments. I'm on my way to research some of the avenues
that you've opened up for me. I'm not necessarily convinced that the
problem is related to garbage collection either. At the time, I just
thought it might be a possibility, hence the statement "Am I correct?"
Nonetheless, we'll see what else I can turn up.

Later on, I may test the GC avenue anyway just to learn a little bit more.
Do you have an example of how I could create a reference to the
object/struct? I haven't been able to find one.

Thanks!

Chris

"Sridhar Panatula" <pa******@hotma il.com> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
Hello Chirs,

I too do not think that, what you are seeing is because of Garbage
Collection.
The easiest way to check whether your Object has been removed or not is to
print values after the Call to DLL.

But, I think its due to the nature of the two different platforms(if I can
call .NET as platform).
When you create an object in .NET its part of the managed application. So I do not think you can pass the reference to Unamanaged app to use that memory address.
Firstly it may not be the true address and secondly the values stored may
not be similar to how values are stored in a plain C++/VB/Windows app. For
example in remoting the objects are transported by Boxing/Unboxing and
similar should happen in your case.

So you should be looking at "How to pass values between managed and
Unmanaged applications"

One somewhat relevant I could find quikcly is
http://msdn.microsoft.com/netframewo...allcomcomp.asp
I am not sure that the above can answer all your questions but I hope it
puts on the right path to solving the issue.

If you still think it is because of the GC the easy way to prevent GC is by having a reference to that Object
and not release until you are done calling your external(unaman ged ) app.
Good luck
Sridhar

"Chris" <c w a n @ n o s p a m - v i g i l . c o m> wrote in message
news:u8******** ******@TK2MSFTN GP10.phx.gbl...
Hmm ... I'm not completely sure either. The interface signature in the

DLL
is as follows:
long GetEvent (Event* pEvent );

When I send 'myEvent' via my C# host application, an address of 0x00000000 is assigned to 'pEvent'.

Chris
"Tu-Thach" <tu*****@yahoo. com> wrote in message
news:01******** *************** *****@phx.gbl.. .
I don't think it is because of Garbage collection. You
might want to check out what GetEvent looks like.

Tu-Thach

>-----Original Message-----
>Hi,
>
>I think I'm having some problems here with garbage
collection. Currently, I
>have the following code:
>
>public struct Event
>
>{
>
>public int timestamp;
>
>public EventType event;
>
>public short device;
>
>public int status;
>
>public int input;
>
>}
>
>and the following method using the above struct:
>
>unsafe private void Message( int msg )
>
>{
>
>Event myEvent = new Event();
>
>while ( GetEvent(myEven t) != -1 )
>
>{
>
>// do some work
>
>}
>
>}
>
>GetEvent is a call to an external DLL written in C++.
When I send the
>myEvent reference to the DLL, I receive
a 'System.NullRef erenceException '.
>I believe this is because that myEvent gets garbage
collected shortly after
>the function call to the dll. Am I correct? How do I
prevent the struct
>(or reference to the struct) from being garbage collected
until after the
>function call is complete?
>
>Thanks in advance,
>
>Chris
>
>
>.
>



Nov 13 '05 #3
Hi Nicholas,

I hope this is what you mean:

=============== =============== ======
Enumerators and Structures:

public enum EventType
{
evtState,
evtMesg,
evtTrans
}

public struct Event
{
public int timestamp;
public EventType event;
public short device;
public int status;
public int inputid;

public Event( int timestamp, EventType event, short device, int status,
int inputid )
{
this.timestamp = timestamp;
this.event = event;
this.device = device;
this.status = status;
this.inputid = inputid;
}
}
=============== =============== ======
DriverUnit.cs:

public class DriverUnit
{
unsafe private void Message ( int msg )
{
Event myEvent = new Event();

while ( Get Event (myEvent) != 1 )
{
// do work
}
}

[DllImport("rDri ver.dll", CharSet=CharSet .Auto)]
private static extern int GetEvent ( Event pEvent );
}

=============== =============== ======
rDriver.cpp:
long GetEvent ( Event* pEvent )
{
dManager* pManager = dManager::GetIn stance();
if ( pManager )
{
return pManager->GetEvent2( pEvent );
}
else
{
return -1;
}
}

=============== =============== ======

long GetEvent2 (Event* pEvent)
{
return itsEventQueue.R emove( pEvent )
}

=============== =============== ======

int virtualQueue<T> ::Remove ( T* element )
{
// itsRemovePoint contains data retrived from hardware in the format of
struct type 'Event'

*element = *itsRemovePoint ;

... // continue running code

// I get a 'System.NullRef erenceException ' at the above line
}
=============== =============== ======

Notes:

The following is the process flow:

In 'Message' function of DriverUnit.cs a struct of type Event is created.
It is then passed through the DLLImport interface to the 'GetEvent' method
that resides in rDriver.cpp which is part of the rDriver.dll. When
'GetEvent' is run, the value of the pointer 'pEvent' is 0x00000000.
Eventually, 'Remove' is called which removes data retrieved from hardware
and assigns it to the location of element. However, I receive a
'System.NullRef erenceException ' when this line of code is run.

Thanks for your help,

Chris
"Nicholas Paldino [.NET/C# MVP]" <ni************ **@exisconsulti ng.com> wrote
in message news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
Chris,

Can you post the definition of the structure and the functions that you are passing the structure to? This would help immensely.
--
- Nicholas Paldino [.NET/C# MVP]
- ni************* *@exisconsultin g.com

"Chris" <c w a n @ n o s p a m - v i g i l . c o m> wrote in message
news:Ou******** ******@TK2MSFTN GP10.phx.gbl...
Hi,

I think I'm having some problems here with garbage collection. Currently,
I
have the following code:

public struct Event

{

public int timestamp;

public EventType event;

public short device;

public int status;

public int input;

}

and the following method using the above struct:

unsafe private void Message( int msg )

{

Event myEvent = new Event();

while ( GetEvent(myEven t) != -1 )

{

// do some work

}

}

GetEvent is a call to an external DLL written in C++. When I send the
myEvent reference to the DLL, I receive a

'System.NullRef erenceException '. I believe this is because that myEvent gets garbage collected shortly

after
the function call to the dll. Am I correct? How do I prevent the struct (or reference to the struct) from being garbage collected until after the function call is complete?

Thanks in advance,

Chris


Nov 13 '05 #4
Hmm ... how incredibly dumb of me ... thanks for your suggestion - it seems
to be working now!

Thanks,

Chris
"100" <sg******@drome ydesign.com> wrote in message
news:OB******** *****@TK2MSFTNG P10.phx.gbl...
Hi Chris,
The problem is not in the GC. As long as it is a struct (value type) the
operator new creates the object as a vlaue-type object in the stack and its memory becomes free as soon as the method ends. I has nothing to do with the GC. The problem, I thing, is in the way you have declared the external
method for the GetEvent function.
As I can see form the next messages on the thread the c++ prototype is
long GetEvent (Event* pEvent );
As long as Event is a value type and the GetEvent wants the addres you
should consider using the following declaration

[DllImport("dll-name")]
public static extern Int32 GetEvent(ref Event pEvent) ;

and calling it as:
while ( GetEvent(ref myEvent) != -1 )
{

}

HTH
B\rgds
100
"Chris" <c w a n @ n o s p a m - v i g i l . c o m> wrote in message
news:Ou******** ******@TK2MSFTN GP10.phx.gbl...
Hi,

I think I'm having some problems here with garbage collection. Currently,
I
have the following code:

public struct Event

{

public int timestamp;

public EventType event;

public short device;

public int status;

public int input;

}

and the following method using the above struct:

unsafe private void Message( int msg )

{

Event myEvent = new Event();

while ( GetEvent(myEven t) != -1 )

{

// do some work

}

}

GetEvent is a call to an external DLL written in C++. When I send the
myEvent reference to the DLL, I receive a

'System.NullRef erenceException '. I believe this is because that myEvent gets garbage collected shortly

after
the function call to the dll. Am I correct? How do I prevent the struct (or reference to the struct) from being garbage collected until after the function call is complete?

Thanks in advance,

Chris


Nov 13 '05 #5

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

Similar topics

13
2016
by: Emmanuel | last post by:
Hi, I run across this problem, and couldn't find any solution (python 2.2.2) : Code : =========== from __future__ import generators >>> class titi:
1
2301
by: Bob | last post by:
Are there any known applications out there used to test the performance of the .NET garbage collector over a long period of time? Basically I need an application that creates objects, uses them, and then throws them away and then monitors the garbage collection and store statistics on it, preferably in C#. I want to know what is the longest...
6
810
by: Ganesh | last post by:
Is there a utility by microsoft (or anyone) to force garbage collection in a process without have access to the process code. regards Ganesh
11
2718
by: Rick | last post by:
Hi, My question is.. if Lisp, a 40 year old language supports garbage collection, why didn't the authors of C++ choose garbage collection for this language? Are there fundamental reasons behind this? Is it because C is generally a 'low level' language and they didn't want garbage collection to creep into C++ and ruin everything? Just...
34
6376
by: Ville Voipio | last post by:
I would need to make some high-reliability software running on Linux in an embedded system. Performance (or lack of it) is not an issue, reliability is. The piece of software is rather simple, probably a few hundred lines of code in Python. There is a need to interact with network using the socket module, and then probably a need to do...
8
3026
by: mike2036 | last post by:
For some reason it appears that garbage collection is releasing an object that I'm still using. The object is declared in a module and instantiated within a class that is in turn instantiated by the mainline. The class that instantiated the object in question is definitely still in existence at the point garbage collection swoops in and...
56
3636
by: Johnny E. Jensen | last post by:
Hellow I'am not sure what to think about the Garbage Collector. I have a Class OutlookObject, It have two private variables. Private Microsoft.Office.Interop.Outlook.Application _Application = null; Private Microsoft.Office.Interop.Outlook.NameSpace _Namespace = null; The Constructor: public OutlookObject()
350
11556
by: Lloyd Bonafide | last post by:
I followed a link to James Kanze's web site in another thread and was surprised to read this comment by a link to a GC: "I can't imagine writing C++ without it" How many of you c.l.c++'ers use one, and in what percentage of your projects is one used? I have never used one in personal or professional C++ programming. Am I a holdover to...
158
7738
by: pushpakulkar | last post by:
Hi all, Is garbage collection possible in C++. It doesn't come as part of language support. Is there any specific reason for the same due to the way the language is designed. Or it is discouraged due to some specific reason. If someone can give inputs on the same, it will be of great help. Regards, Pushpa
0
7693
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...
0
7605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7665
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...
1
5501
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...
0
5217
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...
0
3651
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...
0
3631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2105
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1207
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.