473,386 Members | 1,773 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,386 software developers and data experts.

Memory allocation

I have recently written a .NET wrapper for a C++ DLL file and it seems to be
working well, but I do have some concerns about memory. From my limited C
programming experience (college 15+ years ago), I seem to remember that when
using structures etc in C, you had to allocate memory to them then
deallocate them when you had finished using them. My C# code creates a
variable of type structure, passes it to the C++ DLL and then returns
whatever data is necessary, without deallocating the structure reference. I
tried setting it to null, but the compiler wouldn't accept that as it said
it was a value type. Do I actually need to do anything or does the automatic
garbage collection take care of it for me? One thing I have to avoid is a
memory leak every time I make a call to the DLL. I am using VS 2005 and the
compact framework (2.0). Thanks in advance.

Andy Baker
Jul 21 '08 #1
2 2270
On Jul 21, 1:04*pm, "Andy Baker" <aba...@NOSPAMvanputer.comwrote:
I have recently written a .NET wrapper for a C++ DLL file and it seems tobe
working well, but I do have some concerns about memory. From my limited C
programming experience (college 15+ years ago), I seem to remember that when
using structures etc in C, you had to allocate memory to them then
deallocate them when you had finished using them. My C# code creates a
variable of type structure, passes it to the C++ DLL and then returns
whatever data is necessary, without deallocating the structure reference.I
tried setting it to null, but the compiler wouldn't accept that as it said
it was a value type. Do I actually need to do anything or does the automatic
garbage collection take care of it for me? One thing I have to avoid is a
memory leak every time I make a call to the DLL. I am using VS 2005 and the
compact framework (2.0). Thanks in advance.
First of all, C# structs are not allocated on the heap (except when a
struct is a field of a class), but on the stack. You no more need to
free that than you need to free a local variable of struct type in C/C+
+. You may be confused by having to use the keyword "new" to
initialize the variable, but in C#, it does not perform heap
allocation when applied to value types - rather, it is equivalent to
creating a stack-allocated temporary in C++.

Anyway, if you pass a struct as an argument to an unmanaged function,
you should not need to do any memory management. The only case where
it may be necessary is when the unmanaged function allocates some
memory, and returns pointer to it, assuming the caller will deallocate
- then you'll need to handle this manually.
Jul 21 '08 #2
If you're concerned that memory leaks may be a problem. For example if you
see rising memory allocation in TaskManager while your program is running,
Microsof Research have a new tool LeakDiak that will assist in finding them.

I have recently used this to good effect.

Here's a good blog article.

http://mcfunley.com/cs/blogs/dan/arc...12/11/674.aspx

--
--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
"Andy Baker" <ab****@NOSPAMvanputer.comwrote in message
news:m-idnY92PbRzzRnVnZ2dneKdnZydnZ2d@plusnet...
>I have recently written a .NET wrapper for a C++ DLL file and it seems to
be working well, but I do have some concerns about memory. From my limited
C programming experience (college 15+ years ago), I seem to remember that
when using structures etc in C, you had to allocate memory to them then
deallocate them when you had finished using them. My C# code creates a
variable of type structure, passes it to the C++ DLL and then returns
whatever data is necessary, without deallocating the structure reference. I
tried setting it to null, but the compiler wouldn't accept that as it said
it was a value type. Do I actually need to do anything or does the
automatic garbage collection take care of it for me? One thing I have to
avoid is a memory leak every time I make a call to the DLL. I am using VS
2005 and the compact framework (2.0). Thanks in advance.

Andy Baker

Aug 16 '08 #3

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

Similar topics

6
by: chris | last post by:
Hi all, I need to know, what is the difference between dynamic memory allocation, and stack allocation ? 1. If I have a class named DestinationAddress, when should I use dynamic memory...
4
by: PaulR | last post by:
Hi, We have a Server running SLES 8 and 3GB memory, with 1 DB2 instance and 2 active Databases. General info... DB2level = "DB2 v8.1.0.72", "s040914", "MI00086", and FixPak "7" uname -a =...
74
by: ballpointpenthief | last post by:
If I have malloc()'ed a pointer and want to read from it as if it were an array, I need to know that I won't be reading past the last index. If this is a pointer to a pointer, a common technique...
62
by: ivan.leben | last post by:
How can I really delete a preloaded image from memory/disk cache? Let's say I preload an image by creating an Image object and setting its src attribute to desired URL: var img = new Image();...
66
by: Johan Tibell | last post by:
I've written a piece of code that uses sockets a lot (I know that sockets aren't portable C, this is not a question about sockets per se). Much of my code ended up looking like this: if...
24
by: Ken | last post by:
In C programming, I want to know in what situations we should use static memory allocation instead of dynamic memory allocation. My understanding is that static memory allocation like using array...
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
34
by: jacob navia | last post by:
Suppose that you have a module that always allocates memory without ever releasing it because the guy that wrote it was lazy, as lazy as me. Now, you want to reuse it in a loop. What do you do?...
14
by: vivek | last post by:
i have some doubts on dynamic memory allocation and stacks and heaps where is the dynamic memory allocation used? in function calls there are some counters like "i" in the below function. Is...
66
by: karthikbalaguru | last post by:
Hi, Will 'free' return the memory Immediately to the OS ? Thx in advans, Karthik Balaguru
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...

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.