Click here to Skip to main content
15,892,697 members
Home / Discussions / C#
   

C#

 
AnswerRe: Why doesn't my code find the .SelectedIndex? Pin
OriginalGriff24-Jan-15 21:53
mveOriginalGriff24-Jan-15 21:53 
GeneralRe: Why doesn't my code find the .SelectedIndex? Pin
rfresh25-Jan-15 5:05
rfresh25-Jan-15 5:05 
GeneralRe: Why doesn't my code find the .SelectedIndex? Pin
OriginalGriff25-Jan-15 5:20
mveOriginalGriff25-Jan-15 5:20 
Questionhow to add a image to header of exportinf pdf in devexpress gridview Pin
Member 1064949524-Jan-15 11:04
Member 1064949524-Jan-15 11:04 
QuestionEncoding.ASCII.GetString(byte[]) and CR LF Pin
TMattC24-Jan-15 10:42
TMattC24-Jan-15 10:42 
AnswerRe: Encoding.ASCII.GetString(byte[]) and CR LF Pin
Richard Andrew x6424-Jan-15 16:32
professionalRichard Andrew x6424-Jan-15 16:32 
GeneralRe: Encoding.ASCII.GetString(byte[]) and CR LF Pin
TMattC25-Jan-15 1:32
TMattC25-Jan-15 1:32 
Questionmemory leak calling C functions in C# Pin
jpvi24-Jan-15 2:23
jpvi24-Jan-15 2:23 
I use C dll functions in C#. It works fine except I have a memory leak.

The first C function I use allocates memory (with malloc) and returns an array of string (char***) and the second one frees the memory (using free).

When I test those functions in a C program everything is ok - there are no memory leaks. However when I use those functions in a simple C# program using just thos functions I have a memory leak.

I 'm sure I'm doing something bad but I can't figure out what.

My code :
C#
[DllImport("TestERP.dll", CallingConvention = CallingConvention.StdCall)]
    private static extern int GetArray(string baseval,ref int nb,ref IntPtr array,IntPtr msg);

    [DllImport("TestERP.dll", CallingConvention = CallingConvention.StdCall)]
    private static extern int freeMemory( int nb, ref IntPtr array);


public static void GetManagedArray()
    {
         string[] tmp = new string[12];
         int nb = 0;
         IntPtr msg = Marshal.AllocHGlobal(256);
         string tmp2 = "";
         IntPtr intArr = IntPtr.Zero; 

         //call the C function which return the arry of string intArr
         GetArray("val",ref nb,  ref intArr,msg);

         //copy in managed string array
         for (int i = 0; i < nb; i++)
            tmp[i] = Marshal.PtrToStringAnsi(Marshal.ReadIntPtr(intArr, i * IntPtr.Size));

         tmp2 = Marshal.PtrToStringAnsi(msg);

         Marshal.FreeHGlobal(msg);

         //call the C function whiec free memory
         freeMemory(nb, ref intArr);

         intArr = IntPtr.Zero;

         GC.Collect();

    }

I add the code of C functions :
C++
int __stdcall GetArray(char base[],int* nb,char*** array,char* outmsg)
{
    int i =0;

    (*array) = (char**)calloc(12, sizeof(char*));

    for (i = 0; i < 12; i++)
    {
        (*array)[i] = calloc(256,sizeof(char));
        sprintf((*array)[i],"%s %d",base,i) ;
    }

    *nb= 12;

    strcpy(outmsg,"message erreur");

   return 0;
}

int __stdcall freeMemory(int nb,char*** array)
{
    int i =0;

    for (i = 0; i < nb; i++)
    {
        free((*array)[i]);
        (*array)[i] = NULL;
    }

    free((*array));

    *array = NULL;

    return 0;
}

AnswerRe: memory leak calling C functions in C# Pin
Dave Kreskowiak24-Jan-15 4:26
mveDave Kreskowiak24-Jan-15 4:26 
GeneralRe: memory leak calling C functions in C# Pin
jpvi24-Jan-15 5:11
jpvi24-Jan-15 5:11 
QuestionMvc Fileupload control Pin
Member 1133082524-Jan-15 2:23
Member 1133082524-Jan-15 2:23 
QuestionHow to change background color of a cell in Devexpress Grid when i click select them ? Pin
Member 245846723-Jan-15 22:14
Member 245846723-Jan-15 22:14 
QuestionDifference between Array and Loop. Pin
Member 1116162523-Jan-15 18:44
Member 1116162523-Jan-15 18:44 
AnswerRe: Difference between Array and Loop. Pin
Peter Leow23-Jan-15 18:58
professionalPeter Leow23-Jan-15 18:58 
AnswerRe: Difference between Array and Loop. Pin
PIEBALDconsult23-Jan-15 19:16
mvePIEBALDconsult23-Jan-15 19:16 
AnswerRe: Difference between Array and Loop. Pin
Richard MacCutchan23-Jan-15 22:40
mveRichard MacCutchan23-Jan-15 22:40 
GeneralRe: Difference between Array and Loop. Pin
harold aptroot23-Jan-15 23:15
harold aptroot23-Jan-15 23:15 
AnswerRe: Difference between Array and Loop. Pin
LLLLGGGG24-Jan-15 2:12
LLLLGGGG24-Jan-15 2:12 
AnswerRe: Difference between Array and Loop. Pin
Santosh K. Tripathi26-Jan-15 22:38
professionalSantosh K. Tripathi26-Jan-15 22:38 
QuestionFill dataGrid from MySQL database in C# WPF Pin
cicill23-Jan-15 17:49
cicill23-Jan-15 17:49 
SuggestionRe: Fill dataGrid from MySQL database in C# WPF Pin
Richard MacCutchan23-Jan-15 22:37
mveRichard MacCutchan23-Jan-15 22:37 
AnswerRe: Fill dataGrid from MySQL database in C# WPF Pin
Mycroft Holmes23-Jan-15 23:16
professionalMycroft Holmes23-Jan-15 23:16 
Questionfee system with keep the record of whole year Pin
Haidar ali23-Jan-15 16:56
Haidar ali23-Jan-15 16:56 
AnswerRe: fee system with keep the record of whole year Pin
Dave Kreskowiak23-Jan-15 17:05
mveDave Kreskowiak23-Jan-15 17:05 
QuestionMulti Lingual C# application - Issue on getting Chinese Language selection option on typing English key combination Pin
Johnson Antony23-Jan-15 15:57
Johnson Antony23-Jan-15 15:57 

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.