473,849 Members | 1,779 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 12343
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
2044
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
2340
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 period of time that an application may lock up while garbage collection is processing. Thanks!
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
2750
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 wondering :)
34
6447
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 something hardware- related which will get its own driver written in C.
8
3053
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 yanks it out from under my processing. Is there a way to ensure an instantiated object cannot be freed...
56
3731
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
11968
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 days gone by?
158
7940
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
9893
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
9738
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11005
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...
0
10349
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9501
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...
0
7070
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
5921
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4139
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3177
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.