Intptr的释放问题

jonas401 2009-03-26 09:44:08
C#中申请内存大概有两种方式:
1 IntPtr Marshal.AllocHGlobal(int), 使用Marshal.FreeHGlobal(IntPtr)释放
2 IntPtr Marshal.AllocCoTaskMem(int), 使用Marshal.FreeCoTaskMem(IntPtr)释放

问题:
1 这两种内存方式有什么区别?哪种情况下使用哪种分配方式?
2 在下面这段代码中,是使用何种内存分方式?还是只是类型转换而未重新分析空间?
...
fixed (Int32* ptr = &CMD)
{
Marshal.Copy(new IntPtr(ptr), buffer, pos, size);
}
...
...全文
1131 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
jonas401 2009-03-26
  • 打赏
  • 举报
回复
OK,清楚了
kongdelu2008 2009-03-26
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 gomoku 的回复:]
CoTaskMemAlloc和CoTaskMemFree API一般用在COM交互的情况下。
而Marshal.FreeCoTaskMem(IntPtr)可以释放CoTaskMemAlloc申请的内存。

LocalAlloc和LocalFree API则可对应于Marshal.AllocHGlobal/Marshal.FreeHGlobal
[/Quote]
gomoku 2009-03-26
  • 打赏
  • 举报
回复
CoTaskMemAlloc和CoTaskMemFree API一般用在COM交互的情况下。
而Marshal.FreeCoTaskMem(IntPtr)可以释放CoTaskMemAlloc申请的内存。

LocalAlloc和LocalFree API则可对应于Marshal.AllocHGlobal/Marshal.FreeHGlobal
gomoku 2009-03-26
  • 打赏
  • 举报
回复
只是类型转换
jonas401 2009-03-26
  • 打赏
  • 举报
回复
人呢
格拉 2009-03-26
  • 打赏
  • 举报
回复
帮顶
//创建初始化播放器资源 [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] extern public static IntPtr libvlc_new(int argc, IntPtr argv); //创建播放器实例 [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr libvlc_media_player_new(IntPtr libvlc_instance); // 释放libvlc实例 [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] public static extern void libvlc_release(IntPtr libvlc_instance); //获取库版本信息 [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] public static extern String libvlc_get_version(); // 从视频来源(例如Url)构建一个libvlc_meida RTSP [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr libvlc_media_new_location(IntPtr libvlc_instance, IntPtr path); // 从本地文件路径构建一个libvlc_media rtsp串流不适合调用此接口 // [MarshalAs(UnmanagedType.LPStr)] string path [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr libvlc_media_new_path(IntPtr libvlc_instance, IntPtr path); /// /// 影片长度 /// /// /// [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr libvlc_media_player_get_length(IntPtr libvlc_media_player); //释放对象 [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] public static extern void libvlc_media_release(IntPtr libvlc_media_inst); // 将视频(libvlc_media)绑定到播放器上 [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] public static extern void libvlc_media_player_set_media(IntPtr libvlc_media_player, IntPtr libvlc_media); //创建(libvlc_media)播放窗口 [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] publ
//创建初始化播放器资源 [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] extern public static IntPtr libvlc_new(int argc, IntPtr argv); //创建播放器实例 [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr libvlc_media_player_new(IntPtr libvlc_instance); // 释放libvlc实例 [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] public static extern void libvlc_release(IntPtr libvlc_instance); //获取库版本信息 [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] public static extern String libvlc_get_version(); // 从视频来源(例如Url)构建一个libvlc_meida RTSP [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr libvlc_media_new_location(IntPtr libvlc_instance, IntPtr path); // 从本地文件路径构建一个libvlc_media rtsp串流不适合调用此接口 // [MarshalAs(UnmanagedType.LPStr)] string path [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr libvlc_media_new_path(IntPtr libvlc_instance, IntPtr path); /// /// 影片长度 /// /// /// [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr libvlc_media_player_get_length(IntPtr libvlc_media_player); //释放对象 [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] public static extern void libvlc_media_release(IntPtr libvlc_media_inst); // 将视频(libvlc_media)绑定到播放器上 [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] public static extern void libvlc_media_player_set_media(IntPtr libvlc_media_player, IntPtr libvlc_media); //创建(libvlc_media)播放窗口 [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] public static extern void libvlc_media_player_new_from_media(IntPtr libvlc_media_player); // 设置图像输出的窗口 [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] public static extern void libvlc_media_player_set_hwnd(IntPtr libvlc_mediaplayer, Int32 drawable); //播放 [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] public static extern void libvlc_media_player_play(IntPtr libvlc_mediaplayer); //暂停 [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] public static extern void libvlc_media_player_pause(IntPtr libvlc_mediaplayer); //停止 [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] public static extern void libvlc_media_player_stop(IntPtr libvlc_mediaplayer); // 解析视频资源的媒体信息(如时长等) [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] public static extern void libvlc_media_parse(IntPtr libvlc_media); // 返回视频的时长(必须先调用libvlc_media_parse之后,该函数才会生效) [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] public static extern Int64 libvlc_media_get_duration(IntPtr libvlc_media); // 当前播放的时间 [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] public static extern Int64 libvlc_media_player_get_time(IntPtr libvlc_mediaplayer); // 设置播放位置(拖动) [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] public static extern void libvlc_media_player_set_time(IntPtr libvlc_mediaplayer, Int64 time); /// /// 抓图 /// /// /// 经典0 /// 完整路径,文件名英文或下划线开头 /// /// /// [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] public static extern int libvlc_video_take_snapshot(IntPtr libvlc_mediaplayer, uint num, IntPtr filePath, uint i_width, uint i_height); //media player release [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] public static extern void libvlc_media_player_release(IntPtr libvlc_mediaplayer); // 获取音量 [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] public static extern int libvlc_audio_get_volume(IntPtr libvlc_media_player); //设置音量 [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] public static extern void libvlc_audio_set_volume(IntPtr libvlc_media_player, int volume); // 设置全屏 [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] public static extern void libvlc_set_fullscreen(IntPtr libvlc_media_player, int isFullScreen); /// ///判断是否可以录像 /// /// /// //Can the media player record the current media? [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] public static extern bool libvlc_media_player_is_recordable(IntPtr libvlc_media_player); /// ///判断是否在录像 /// /// /// [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] public static extern bool libvlc_media_player_is_recording(IntPtr libvlc_media_player); /// /// 录像开始 /// /// /// 保存路径+文件名(d:\\record (将在D盘根目录保存为record.mp4)) /// [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] public static extern int libvlc_media_player_record_start(IntPtr libvlc_media_player, IntPtr psz_PathFilename); /// /// 录像停止 /// /// /// [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] public static extern int libvlc_media_player_record_stop(IntPtr libvlc_media_player);

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧