找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1984|回复: 2

[求助] 如何将resbuf类型形参转为实参

[复制链接]
发表于 2013-6-2 12:14:29 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
本帖最后由 Gdlprfcu 于 2013-6-2 12:17 编辑

学习C++过程碰到好多难题,C++对内存的操作实在是太麻烦了,最近想做一个全局的选择过虑器给LISP调用,由于传进来的链表调用完就会被释放掉,但我在反应器里还得用到,此时就会出错,试了好久都没成功搞好,还望高手能帮帮小弟。
下面是我写的一段代码,FilterRb 是类成员指针变量,不知为何如果多次调用此方法,发现FilterRb 的值是会变的,一时是第一次调用时的值一时又是第二次调用的值,且释放FilterRb 也会出错,望高手能指点一下,应该如何做。
  1. int SetFilter(struct resbuf  *rb)
  2.   {
  3.     if (rb == NULL)
  4.     {
  5.       if (FilterRb != NULL)
  6.       {
  7.         acutRelRb(FilterRb);
  8.         FilterRb = NULL;
  9.       }
  10.       return RTNORM;
  11.     }
  12.     if (FilterRb != NULL)
  13.     {
  14.       if (RTNORM != acutRelRb(FilterRb))
  15.       {
  16.         return ERROR;
  17.       }
  18.       FilterRb = NULL;
  19.     }
  20.    
  21.     if (rb->restype != RTLB)
  22.     {
  23.       return ERROR;
  24.     }
  25.     struct resbuf *tmrb = NULL;
  26.     while (  (rb = rb->rbnext) != NULL && rb->restype != RTLE)
  27.     {
  28.       struct resbuf *newrb;
  29.       /*int rbtype;*/
  30.       if ((rb->restype >= 0 && rb->restype <= 9)||(rb->restype >= 999 && rb->restype <= 1005) || rb->restype == -4 )
  31.       {
  32.         newrb = acutNewRb(rb->restype);
  33.         newrb->resval.rstring = (ACHAR *)malloc(wcslen(rb->resval.rstring)+ 1);
  34.         wcscpy(newrb->resval.rstring,rb->resval.rstring);
  35.       }
  36.       else if ((rb->restype >= 10 && rb->restype <= 19)||(rb->restype >= 210 && rb->restype <= 239))
  37.       {
  38.         ads_point pt;
  39.         ads_point_set(rb->resval.rpoint,pt);
  40.         newrb = acutNewRb(rb->restype);
  41.         newrb->resval.rpoint[X] = pt[X];
  42.         newrb->resval.rpoint[Y] = pt[Y];
  43.         newrb->resval.rpoint[Z] = pt[Z];
  44.       }
  45.       else if (rb->restype >= 38 && rb->restype <= 59)
  46.       {
  47.         newrb = acutNewRb(rb->restype);
  48.         newrb->resval.rreal = rb->resval.rreal;
  49.       }
  50.       else if (rb->restype >= 60 && rb->restype <= 79)
  51.       {
  52.         newrb = acutNewRb(rb->restype);
  53.         newrb->resval.rint = rb->resval.rint;
  54.       }
  55.       else if(rb->restype == -3)
  56.       {
  57.         newrb = acutNewRb(rb->restype);
  58.       }
  59.       else
  60.       {
  61.         if (FilterRb != NULL)
  62.         {
  63.           acutRelRb(FilterRb);
  64.           FilterRb = NULL;
  65.         }
  66.         return RTERROR;
  67.       }
  68.       if (FilterRb == NULL)
  69.       {
  70.         FilterRb = tmrb = newrb;
  71.       }
  72.       else
  73.       {
  74.         tmrb->rbnext = newrb;
  75.         tmrb = tmrb->rbnext;
  76.       }
  77.     }
  78.     return RTNORM;
  79.   }






论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!

已领礼包: 13个

财富等级: 恭喜发财

发表于 2013-6-2 14:29:08 | 显示全部楼层
把你的FilterRB 定义的那个 类 的代码也贴上来啊。
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-6-2 16:12:05 | 显示全部楼层
反应器我是用工具条添加的,MySelectFilter是这个反应器的文档级变量。
这是h文件
  1. //-----------------------------------------------------------------------------
  2. //----- SelectFilter.h : Declaration of the SelectFilter
  3. //-----------------------------------------------------------------------------
  4. #pragma once

  5. #ifdef MY_MODULE
  6. #define DLLIMPEXP __declspec(dllexport)
  7. #else
  8. //----- Note: we don't use __declspec(dllimport) here, because of the
  9. //----- "local vtable" problem with msvc. If you use __declspec(dllimport),
  10. //----- then, when a client dll does a new on the class, the object's
  11. //----- vtable pointer points to a vtable allocated in that client
  12. //----- dll. If the client dll then passes the object to another dll,
  13. //----- and the client dll is then unloaded, the vtable becomes invalid
  14. //----- and any virtual calls on the object will access invalid memory.
  15. //-----
  16. //----- By not using __declspec(dllimport), we guarantee that the
  17. //----- vtable is allocated in the server dll during the ctor and the
  18. //----- client dll does not overwrite the vtable pointer after calling
  19. //----- the ctor. And, since we expect the server dll to remain in
  20. //----- memory indefinitely, there is no problem with vtables unexpectedly
  21. //----- going away.
  22. #define DLLIMPEXP
  23. #endif

  24. //-----------------------------------------------------------------------------
  25. #include "acssgetfilter.h"
  26. #include "afx.h"
  27. //-----------------------------------------------------------------------------
  28. //----- Note: Uncomment the DLLIMPEXP symbol below if you wish exporting
  29. //----- your class to other ARX/DBX modules
  30. class /*DLLIMPEXP*/ SelectFilter : public AcEdSSGetFilter {

  31. protected:
  32.         AcApDocument *mpDoc ;

  33. public:
  34.         //CString addFilterpname;
  35.        
  36.         SelectFilter (AcApDocument *pDoc =acDocManager->curDocument (),const ACHAR *addFilterpname = NULL) ;
  37.         SelectFilter (AcApDocument *pDoc =acDocManager->curDocument (),const struct resbuf *Rb = NULL) ;
  38.         virtual ~SelectFilter () ;
  39.         virtual void Attach () ;
  40.         virtual void Detach () ;
  41.         virtual AcApDocument *Subject () const { return (mpDoc) ; }
  42.         virtual bool IsAttached () const { return (mpDoc != NULL) ; }

  43.         virtual void ssgetAddFilter (
  44.                 int ssgetFlags,
  45.                 AcEdSelectionSetService &service,
  46.                 const AcDbObjectIdArray &selectionSet,
  47.                 const AcDbObjectIdArray &subSelectionSet
  48.         ) ;
  49.         static int addSetectFilter(struct resbuf *rb);
  50.         static int removeSetectFilter(struct resbuf *rb);
  51.         static int addFilter(struct resbuf *rb);
  52.         int SetPFuncName(ACHAR * pName)//设置回调函数方法
  53.         {
  54.                 if (pName == NULL)
  55.                 {
  56.                         delete addFilterpname;
  57.                         addFilterpname = NULL;
  58.                         return RTNORM;
  59.                 }
  60.                 if (addFilterpname != NULL)
  61.                 {
  62.                         delete[] addFilterpname;
  63.                         addFilterpname = NULL;
  64.                         //addFilterpname =(ACHAR*) realloc(addFilterpname , wcslen(pName) + 1);
  65.                 }
  66.                 addFilterpname = new ACHAR[wcslen(pName)+ 1];
  67.                 wcscpy(addFilterpname,pName);
  68.                 return RTNORM;
  69.         }
  70.         int SetFilter(struct resbuf  *rb)//设置过虑链表,我把方法直接写这里不知对不对。
  71.         {
  72.                 if (rb == NULL)
  73.                 {
  74.                         if (FilterRb != NULL)
  75.                         {
  76.                                 acutRelRb(FilterRb);
  77.                                 FilterRb = NULL;
  78.                         }
  79.                         return RTNORM;
  80.                 }
  81.                 if (FilterRb != NULL)
  82.                 {
  83.                         if (RTNORM != acutRelRb(FilterRb))
  84.                         {
  85.                                 return ERROR;
  86.                         }
  87.                         FilterRb = NULL;
  88.                 }
  89.                
  90.                 if (rb->restype != RTLB)
  91.                 {
  92.                         return ERROR;
  93.                 }
  94.                 struct resbuf *tmrb = NULL;
  95.                 while (  (rb = rb->rbnext) != NULL && rb->restype != RTLE)
  96.                 {
  97.                         struct resbuf *newrb;
  98.                         /*int rbtype;*/
  99.                         if ((rb->restype >= 0 && rb->restype <= 9)||(rb->restype >= 999 && rb->restype <= 1005) || rb->restype == -4 )
  100.                         {
  101.                                 newrb = acutNewRb(rb->restype);
  102.                                 newrb->resval.rstring = (ACHAR *)malloc(wcslen(rb->resval.rstring)+ 1);
  103.                                 wcscpy(newrb->resval.rstring,rb->resval.rstring);
  104.                         }
  105.                         else if ((rb->restype >= 10 && rb->restype <= 19)||(rb->restype >= 210 && rb->restype <= 239))
  106.                         {
  107.                                 ads_point pt;
  108.                                 ads_point_set(rb->resval.rpoint,pt);
  109.                                 newrb = acutNewRb(rb->restype);
  110.                                 newrb->resval.rpoint[X] = pt[X];
  111.                                 newrb->resval.rpoint[Y] = pt[Y];
  112.                                 newrb->resval.rpoint[Z] = pt[Z];
  113.                         }
  114.                         else if (rb->restype >= 38 && rb->restype <= 59)
  115.                         {
  116.                                 newrb = acutNewRb(rb->restype);
  117.                                 newrb->resval.rreal = rb->resval.rreal;
  118.                         }
  119.                         else if (rb->restype >= 60 && rb->restype <= 79)
  120.                         {
  121.                                 newrb = acutNewRb(rb->restype);
  122.                                 newrb->resval.rint = rb->resval.rint;
  123.                         }
  124.                         else if(rb->restype == -3)
  125.                         {
  126.                                 newrb = acutNewRb(rb->restype);
  127.                         }
  128.                         else
  129.                         {
  130.                                 if (FilterRb != NULL)
  131.                                 {
  132.                                         acutRelRb(FilterRb);
  133.                                         FilterRb = NULL;
  134.                                 }
  135.                                 return RTERROR;
  136.                         }
  137.                         if (FilterRb == NULL)
  138.                         {
  139.                                 FilterRb = tmrb = newrb;
  140.                         }
  141.                         else
  142.                         {
  143.                                 tmrb->rbnext = newrb;
  144.                                 tmrb = tmrb->rbnext;
  145.                         }
  146.                 }
  147.                 return RTNORM;
  148.         }

  149. private:
  150.         ACHAR *addFilterpname;//成员变量回调函数名称字符串
  151.         struct resbuf *FilterRb;//成员变量过虑链表
  152. } ;

这是CPP文件
  1. //-----------------------------------------------------------------------------
  2. //----- SelectFilter.cpp : Implementation of SelectFilter
  3. //-----------------------------------------------------------------------------
  4. #include "StdAfx.h"
  5. #include "SelectFilter.h"
  6. #include "DocData.h"

  7. //-----------------------------------------------------------------------------
  8. SelectFilter::SelectFilter (AcApDocument *pDoc,ACHAR *pname,resbuf *rb) : AcEdSSGetFilter ()
  9. {
  10.         addFilterpname = NULL;
  11.         if (pname != NULL)
  12.         {
  13.                 addFilterpname = new ACHAR(wcslen(pname));
  14.                 wcscpy(addFilterpname,pname);
  15.         }
  16.         FilterRb = NULL;
  17.         if (rb != NULL)
  18.         {

  19.         }
  20.         mpDoc =NULL ;
  21.         if ( pDoc != NULL ) {
  22.                 mpDoc =pDoc ;
  23.                 addSSgetFilterInputContextReactor (mpDoc, this) ;
  24.         }
  25. }

  26. //-----------------------------------------------------------------------------
  27. SelectFilter::~SelectFilter ()
  28. {
  29.         Detach () ;
  30.         if (addFilterpname != NULL)
  31.         {
  32.                 delete[] addFilterpname;
  33.                 addFilterpname = NULL;
  34.         }
  35.         if (FilterRb != NULL)
  36.         {
  37.                 acutRelRb(FilterRb);//到这里总是出错。
  38.                 FilterRb = NULL;
  39.         }
  40. }

  41. //-----------------------------------------------------------------------------
  42. void SelectFilter::Attach ()
  43. {
  44.         Detach () ;
  45.         mpDoc =acDocManager->curDocument () ;
  46.         if ( mpDoc )
  47.                 addSSgetFilterInputContextReactor (mpDoc, this) ;
  48. }

  49. void SelectFilter::Detach ()
  50. {
  51.         if ( mpDoc ) {
  52.                 removeSSgetFilterInputContextReactor (mpDoc, this) ;
  53.                 mpDoc =NULL ;
  54.         }
  55. }

  56. //-----------------------------------------------------------------------------
  57. void SelectFilter::ssgetAddFilter (int ssgetFlags,AcEdSelectionSetService &service,const AcDbObjectIdArray &selectionSet,const AcDbObjectIdArray &subSelectionSet)
  58. {
  59.         if (addFilterpname == NULL && FilterRb == NULL)
  60.         {
  61.                 return;
  62.         }
  63.         ads_name eName;
  64.         bool bol;
  65.         for (int i = 0;i< subSelectionSet.length();i++)
  66.         {
  67.                 bol = false;
  68.                 struct resbuf *rb = NULL;
  69.                 service.ssNameX(true,i,rb);
  70.                 if (rb != NULL)
  71.                 {
  72.                         struct resbuf *temrb=rb;
  73.                         while(temrb != NULL)
  74.                         {
  75.                                 if(temrb->restype == RTENAME)
  76.                                 {
  77.                                         eName[0]=temrb->resval.rlname[0];
  78.                                         eName[1]=temrb->resval.rlname[1];
  79.                                         if (addFilterpname != NULL)
  80.                                         {
  81.                                                 struct resbuf *in_rb = acutBuildList(RTSTR,addFilterpname,RTENAME,eName,RTNONE);
  82.                                                 if(in_rb == NULL)
  83.                                                 {
  84.                                                         acutPrintf(_T("\n程序出错."));
  85.                                                         return;
  86.                                                 }
  87.                                                 struct resbuf *out_rb = NULL;
  88.                                                 int ret = acedInvoke(in_rb,&out_rb);
  89.                                                 if (ret != RTNORM || out_rb == NULL)
  90.                                                 {
  91.                                                         acutPrintf(_T("\n回调函数错误."));
  92.                                                         return;
  93.                                                 }
  94.                                                 if (out_rb->restype == RTNIL)
  95.                                                 {
  96.                                                         bol = true;
  97.                                                 }
  98.                                         }

  99.                                         if (!bol && FilterRb != NULL)
  100.                                         {
  101.                                                 struct resbuf *endata;
  102.                                                 endata = acdbEntGet(eName);
  103.                                         }
  104.                                 }
  105.                                 temrb = temrb->rbnext;
  106.                         }
  107.                 }
  108.                 if (bol)
  109.                 {
  110.                         service.remove(i);
  111.                 }
  112.         }
  113. }

  114. int SelectFilter::addSetectFilter(struct resbuf *rb)//这个方法添加回调函数返回真假来过虑对象
  115. {
  116.         if(rb == NULL || rb->restype != RTSTR)
  117.         {
  118.                 return RTERROR;
  119.         }
  120.         if (DocVars.docData().MySelectFilter != NULL)
  121.         {
  122.                 DocVars.docData().MySelectFilter->SetPFuncName(rb->resval.rstring);
  123.                 acedRetStr(rb->resval.rstring);
  124.                 return RTNORM;
  125.                 //delete DocVars.docData().MySelectFilter;
  126.                 //DocVars.docData().MySelectFilter = NULL;
  127.         }
  128.         DocVars.docData().MySelectFilter = new SelectFilter();
  129.         DocVars.docData().MySelectFilter->SetPFuncName(rb->resval.rstring);
  130.         acedRetStr(rb->resval.rstring);
  131.         return RTNORM;
  132. }


  133. int SelectFilter::removeSetectFilter(struct resbuf *rb)//移除反应器
  134. {
  135.         if (DocVars.docData().MySelectFilter != NULL)
  136.         {
  137.                 delete DocVars.docData().MySelectFilter;
  138.                 DocVars.docData().MySelectFilter = NULL;
  139.         }
  140.         return RTNORM;
  141. }

  142. int SelectFilter::addFilter(struct resbuf *rb)//这个方法想添加一个和SSGET过虑链表一样的过虑器
  143. {
  144.         if (NULL != DocVars.docData().MySelectFilter)
  145.         {
  146.                 delete DocVars.docData().MySelectFilter;
  147.         }
  148.         DocVars.docData().MySelectFilter = new SelectFilter();
  149.         if (RTNORM != DocVars.docData().MySelectFilter->SetFilter(rb))
  150.         {
  151.                 delete DocVars.docData().MySelectFilter;
  152.                 acutPrintf(_T("\n过虑列表错误。"));
  153.                 acedRetNil();
  154.                 return ERROR;
  155.         }
  156.         acedRetT();
  157.         return RTNORM;
  158. }
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-9-24 10:14 , Processed in 0.194346 second(s), 35 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表