Click here to Skip to main content
15,903,012 members
Home / Discussions / C#
   

C#

 
AnswerRe: How can I read the file's content on server? Pin
Judah Gabriel Himango20-Nov-07 8:07
sponsorJudah Gabriel Himango20-Nov-07 8:07 
GeneralRe: How can I read the file's content on server? Pin
pcphuc20-Nov-07 14:14
pcphuc20-Nov-07 14:14 
GeneralRe: How can I read the file's content on server? Pin
pcphuc20-Nov-07 14:43
pcphuc20-Nov-07 14:43 
GeneralRe: How can I read the file's content on server? Pin
pcphuc20-Nov-07 17:10
pcphuc20-Nov-07 17:10 
GeneralRe: How can I read the file's content on server? Pin
Judah Gabriel Himango21-Nov-07 5:16
sponsorJudah Gabriel Himango21-Nov-07 5:16 
QuestionPreventing Control Painting Pin
DahrkDaiz20-Nov-07 5:34
DahrkDaiz20-Nov-07 5:34 
AnswerRe: Preventing Control Painting Pin
Skippums20-Nov-07 6:02
Skippums20-Nov-07 6:02 
QuestionFree unmanaged memory issue Pin
sampdoria20-Nov-07 5:33
sampdoria20-Nov-07 5:33 
Hi,
it's my first post here and I hope that someone can help me.

I'm developing an application in C#, using VisualStudio .NET 2005, which calls same functions from an ANSI C library.

My problems involve out of memory exceptions :S, because i don't really understand how to use the methods DestroyStructure() and FreeCoTaskMem().

I had 3 different kind of C# classes:
<br />
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]<br />
public class myClassA<br />
{<br />
        public Int32 n_elem;<br />
    <br />
        public IntPtr temperature = IntPtr.Zero;<br />
<br />
        public IntPtr length = IntPtr.Zero;<br />
}<br />
<br />
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]<br />
public class myClassB<br />
{<br />
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]<br />
        public char[] identifier = new char[8];<br />
        <br />
        public Double weight = 0F;<br />
<br />
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]<br />
        public Double[] coeffs = new Double[3];<br />
}<br />
<br />
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]<br />
public myClassC<br />
{<br />
       public IntPtr ptrClassA;<br />
<br />
       public IntPtr ptrClassB;<br />
}<br />

First of all I create an object of myClassA and one of myClassB; then I correctly fill each members with appropriate data, then using Marshal.StructureToPtr() let ptrClassA point the myClassA object just created and the same for ptrClassB.
The line before calling the C dll I let a new IntPtr to reference the myClassC object, and then call the library

<br />
myClassA classAObject = new myClassA();<br />
myClassB classBObject = new myClassB();<br />
myClassC classCObject = new myClassC();<br />
<br />
classAObject.n_elem = rowsCount;//it is the rows number returning from a  SELECT in ORACLE<br />
Double[] temp = new Double[rowsCount];<br />
Double[] len = new Double[rowsCount];<br />
//loop filling temp and len with query results<br />
{...}<br />
<br />
classAObject.temperature = ConvertEx.ArrayToPtr(temp) ;<br />
classAObject.length = ConvertEx.ArrayToPtr(len);  <br />
<br />
classCObject.ptrClassA = ConvertEx.StructToPtr(classAObject );<br />
classCObject.ptrClassB = ConvertEx.StructToPtr(classBObject );<br />
<br />
IntPtr ptrIn = ConvertEx.StructToPtr(classCObject);<br />
<br />
CLibrary(ptrIn);<br />
<br />


StructToPtr and ArrayToPtr are static methods of a class ConvertEx which boxes the basic step to allocate unmanaged memory

<br />
 public static IntPtr StructToPtr(Object oInstance)<br />
       {<br />
            IntPtr ret = IntPtr.Zero;<br />
            Int32 size = Marshal.SizeOf(oInstance.GetType());<br />
            ret = Marshal.AllocCoTaskMem(size);<br />
            Marshal.StructureToPtr(oInstance, ret, true);<br />
            return ret;<br />
        }<br />
<br />
public static IntPtr ArrayToPtr(Int32[] oInstance)<br />
        {<br />
            Int32 elems = oInstance.GetLength(0);<br />
            IntPtr ret = IntPtr.Zero;<br />
            Int32 size = Marshal.SizeOf(oInstance[0].GetType())*elems;<br />
            ret = Marshal.AllocCoTaskMem(size);<br />
            Marshal.Copy(oInstance, 0, ret, elems);<br />
            return ret;<br />
        }<br />
<br />


Then i just call FreeCoTaskMem() for each IntPtr variable, but in fact memory is not correctly released, I think but i cannot find the right way i had to use DestroyStructure() and FreeCoTaskMem() .
Can anyone explain me the difference between DestroyStructure and FreeCoTaskMem or how to use in my application because in some MSDN examples it uses both methods: first DesroyStructure is called and then FreeCoTaskMem.

thanks in advance

cicciollo
Questioninteresting topic plz help Pin
sivaramireddy p20-Nov-07 4:57
sivaramireddy p20-Nov-07 4:57 
AnswerYou must be kidding! Pin
leckey20-Nov-07 5:08
leckey20-Nov-07 5:08 
AnswerRe: interesting topic plz help Pin
duncanmhor20-Nov-07 5:38
duncanmhor20-Nov-07 5:38 
AnswerRe: interesting topic plz help Pin
MasterSharp20-Nov-07 10:42
MasterSharp20-Nov-07 10:42 
GeneralRe: interesting topic plz help Pin
Pete O'Hanlon20-Nov-07 11:07
mvePete O'Hanlon20-Nov-07 11:07 
GeneralRe: interesting topic plz help Pin
MasterSharp23-Nov-07 17:45
MasterSharp23-Nov-07 17:45 
Questionhow to send value in port(tcp/ip) Pin
sivaramireddy p20-Nov-07 4:50
sivaramireddy p20-Nov-07 4:50 
AnswerRe: how to send value in port(tcp/ip) Pin
Colin Angus Mackay20-Nov-07 4:56
Colin Angus Mackay20-Nov-07 4:56 
GeneralRe: how to send value in port(tcp/ip) Pin
Frank Kerrigan20-Nov-07 5:17
Frank Kerrigan20-Nov-07 5:17 
GeneralRe: how to send value in port(tcp/ip) Pin
Colin Angus Mackay20-Nov-07 5:20
Colin Angus Mackay20-Nov-07 5:20 
GeneralRe: how to send value in port(tcp/ip) Pin
Frank Kerrigan20-Nov-07 5:25
Frank Kerrigan20-Nov-07 5:25 
AnswerRe: how to send value in port(tcp/ip) Pin
led mike20-Nov-07 4:57
led mike20-Nov-07 4:57 
AnswerRe: how to send value in port(tcp/ip) Pin
Frank Kerrigan20-Nov-07 5:15
Frank Kerrigan20-Nov-07 5:15 
QuestionVisual Studio 2008 - Tip of the Day Pin
Peter Vertes20-Nov-07 4:36
Peter Vertes20-Nov-07 4:36 
AnswerRe: Visual Studio 2008 - Tip of the Day Pin
Pete O'Hanlon20-Nov-07 11:10
mvePete O'Hanlon20-Nov-07 11:10 
QuestionButton Pin
RussBus20-Nov-07 4:10
RussBus20-Nov-07 4:10 
AnswerRe: Button Pin
Colin Angus Mackay20-Nov-07 4:55
Colin Angus Mackay20-Nov-07 4:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.