Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7541725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:58:10+00:00 2026-05-30T07:58:10+00:00

I am calling a method from C# like this: [DllImport(@C:\Hash.dll, CallingConvention = CallingConvention.Cdecl)] public

  • 0

I am calling a method from C# like this:

[DllImport(@"C:\Hash.dll",
    CallingConvention = CallingConvention.Cdecl)]
    public static extern IntPtr ph_dct_videohash(
        string file,
        ref int length);


    static void Main(string[] args)
    {
            int length = 0;
            ph_dct_videohash(@"C:\Users\shady\H.avi", ref length);
            Console.Read();

    }

And here is the method I am calling from the library

ulong64* ph_dct_videohash(const char *filename, int &Length){

CImgList<uint8_t> *keyframes = ph_getKeyFramesFromVideo(filename);
if (keyframes == NULL)
    return NULL;

Length = keyframes->size();

ulong64 *hash = (ulong64*)malloc(sizeof(ulong64)*Length);
//some code to fill the hash array
return hash;}

The question is how can I retrieve the unsigned 64bit Long array in C# and free the memory after using it. It would be even better if it is managed by the garbage collector for me.

I tried Marshal.copy but it didn’t work and I am afraid there would be a mem leak (I don’t know whether the mem will be freed automatically or not). Any help would be appreciated. Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T07:58:11+00:00Added an answer on May 30, 2026 at 7:58 am

    You have two ways that you can follow:

    1. Add “free” function to your library and call it explicitly for each resource allocated on library’s side. This requires the least amount of code changes on library side, but you have to remember about freeing the memory, unless you’ll create some IDisposable wrapper for IntPtr that will do the job automatically. For example, you could use this class:

    2. Change public library’s functions to accept pointers to buffers instead of allocating them internally. This potentially requires lots of changes on library side, but simplifies the code on C# side.

    EDIT: as a suppliment for the first suggestion, you could use following class:

    public class HashDllAutoPtr : IDisposable
    {
        [DllImport(@"C:\Hash.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern void ph_dct_free(IntPtr ptr);
    
        public HashDllAutoPtr(IntPtr ptr)
        {
            Ptr = ptr;
        }
    
        ~HashDllAutoPtr()
        {
            Dispose();
        }
    
        public IntPtr Ptr
        {
            get;
            private set;
        }
    
        #region IDisposable Members
    
        public void Dispose()
        {
            if (Ptr != IntPtr.Zero)
            {
                ph_dct_free(Ptr);
            }
            Ptr = IntPtr.Zero;
        }
    
        #endregion
    }
    

    Just create an instance of it for each IntPtr result. You can then either call Dispose manually when you want the memory to be freed, or you can just leave it hanging and it will be picked up by GC eventually (although I don’t recommend it). You can read more about disposing unmanaged data here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am calling a C# method from C code. The C# method: [UnmanagedFunctionPointer(CallingConvention.Cdecl)] public
I'm calling a method from an external library with a (simplified) signature like this:
Im using LuaObjcBridge and I'm calling a method from Lua like this: local position
I have a problem calling a static method from a class. Let me explain
i have trouble with calling method from another class this is my code this
I am calling a method from another method and keep getting a warning: firstViewController
I'm having an issue calling a method from a separate project within the same
I know that calling a virtual method from a base class constructor can be
from flex, when calling a .net web method that returns a custom class, I
What are the problems that calling this method can help with? Do you ever

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.