473,396 Members | 2,009 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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(myEvent) != -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.NullReferenceException'.
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 12295
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(unamanged ) 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**************@TK2MSFTNGP10.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(myEvent) != -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.NullReferenceException'.
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******@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.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(unamanged ) 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**************@TK2MSFTNGP10.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(myEvent) != -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.NullReferenceException'.
>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("rDriver.dll", CharSet=CharSet.Auto)]
private static extern int GetEvent ( Event pEvent );
}

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

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

long GetEvent2 (Event* pEvent)
{
return itsEventQueue.Remove( 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.NullReferenceException' 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.NullReferenceException' when this line of code is run.

Thanks for your help,

Chris
"Nicholas Paldino [.NET/C# MVP]" <ni**************@exisconsulting.com> wrote
in message news:%2****************@TK2MSFTNGP12.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**************@exisconsulting.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**************@TK2MSFTNGP10.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(myEvent) != -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.NullReferenceException'. 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******@dromeydesign.com> wrote in message
news:OB*************@TK2MSFTNGP10.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**************@TK2MSFTNGP10.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(myEvent) != -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.NullReferenceException'. 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
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
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...
6
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
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...
34
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,...
8
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...
56
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 =...
350
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...
158
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.