gettype() raise error?



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 20/08/2007 at 05:26, xxxxxxxx wrote:

    User Information:
    Cinema 4D Version:   9.507 
    Platform:   Windows  ;   
    Language(s) :     C++  ;

    ---------
    I want to find out all cameras from active scene. I use the following code:
    void GetAllCameras(AtomArray *oCams,BaseObject *oCam)
    {
         if(oCam)
         {
        MessageDialog("GetType Begin");
              if( oCam->GetType() == Ocamera)
        {
         MessageDialog("Is Ocamera");
                   oCams->Append(oCam);
        }
         MessageDialog("GetDown Begin");
              if(oCam->GetDown())
        {
                   GetAllCameras(oCams,oCam->GetDown());
        }
        MessageDialog("GetDown End");
              if(oCam->GetNext())
        {
                   GetAllCameras(oCams,oCam->GetNext());
        }
         MessageDialog("GetNext End");
         }
    }
    LONG GetAllCameras(AtomArray * oCams )
    {
         if(!oCams)
      {
       //MessageDialog("Alloc AtomArray Fail that Can't get Camera ");
       return 0;
      }
         BaseDocument *doc=GetActiveDocument();
         BaseObject *obj = doc->GetFirstObject();
         if(obj)
         {
        MessageDialog("To  get Camera ");
              GetAllCameras(oCams,obj);
         }
         return oCams->GetCount();
    }

    void SnapCamera( AtomArray *oCams, LONG lCamCnt)
    {
     LONG nCount = 0;
     if ( lCamCnt == 0)
     {
      return;
     }
     BaseDocument* doc = GetActiveDocument();
     MessageDialog( LongToString(lCamCnt) );
        for(LONG i = 0;i < lCamCnt; i++)
        {
            BaseObject *obj=(BaseObject * )oCams->GetIndex(i);
            MessageDialog(obj->GetName());
      if ( !obj) break;
           
      String camname = obj->GetName();
      LONG iLen = camname.GetCStringLen(St8bit)+1;
      char* pName = bNew CHAR[iLen];
       
      camname.GetCString( pName,iLen);
      BaseObject * oldcam = doc->GetRenderBaseDraw()->GetSceneCamera( doc );
      doc->GetRenderBaseDraw()->SetSceneCamera( obj );
        
      Snapshoot( pName ); //perspective is snapshoot name
      bDelete(pName);
     
        }
     
    void GetCameraSnapshoot( void )

     //SetDisplayMode( TRUE );
     AtomArray* oCameras = AtomArray::Alloc();
     if ( !oCameras )
     {
      MessageDialog( "AtomArray Alloc Is Fail" );
      return ;
     }
     //MessageDialog( "AtomArray Alloc Is Succuss" );
      
        LONG lCamCnt = GetAllCameras(oCameras);
     MessageDialog( "SnapCamera" );
     SnapCamera( oCameras, lCamCnt );
     
     AtomArray::Free(oCameras) ;
     
     return ;
    }
    but obj->gettype() raise error:
    Unhandled exception at 0x0081a299 in CINEMA 4D.exe: 0xC0000005: Access violation reading location 0x00000004.
    Then it crash the C4D application.
    Can anyone give hints on how to solve it?
    Thanks in advance.
    Kaviel



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 20/08/2007 at 08:08, xxxxxxxx wrote:

    It´s the same problem most probably then in your other thread. the object that you are access doesn´t seem to be there.

    In your code GetAllCameras you check for

    if(cam)

    but in the next call you ask for

    if(cam->GetDown())

    So in the second call, if cam is NULL, you will get a crash like that you have there. cam could be NULL and you still call a member function of it. This will crash for most probably.



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 20/08/2007 at 10:35, xxxxxxxx wrote:

    Code still seems correct.

    if (ocam)  
    {  
         if (ocam->GetDown())  
         {  
              ...  
         }  
         ...  
    }
    

    This will validate ocam before referencing it in the next condition. As long as the nesting or order is correct, it should work. For instance, this works despite its apparent lack of caution: ;)

    cam = bc->GetObjectLink(MYID, doc);
    if (cam && cam->GetDown()) ...

    It works because the condition checks from left to right (and both conditions must be met in that order). If cam is NULL, the condition immediately fails and cam->GetDown() is never checked (thankfully!).

    This, of course, cannot protect against scrambled memory, memory overruns, or incorrect casts. Sometimes errors occur because one is not being cautious somewhere else (in array ranges for instance). They can be a bugger to uncover in these cases.



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 20/08/2007 at 11:26, xxxxxxxx wrote:

    oh I didn´t see it was nested at all! no need for an explanation, I´m aware of the basics. thx :D



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 20/08/2007 at 12:06, xxxxxxxx wrote:

    No problem. I had to count the braces myself to verify the nesting. :)



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 21/08/2007 at 19:58, xxxxxxxx wrote:

    Hi ...

    Here is some code snip for you ...

    void GetAllCameras(AtomArray *oCams,BaseObject *oCam)
    {
         if(oCam)
         {
              if( oCam->GetType() == Ocamera)
              {
                   oCams->Append(oCam);
              }

    if(oCam->GetDown())
              {
                   GetAllCameras(oCams,oCam->GetDown());
              }

    if(oCam->GetNext())
              {
                   GetAllCameras(oCams,oCam->GetNext());
              }
         }
    }

    LONG GetAllCameras(AtomArray * oCams )
    {
         if(!oCams)
         {
              return 0;
         }

    BaseDocument *doc=GetActiveDocument();
         BaseObject *obj = doc->GetFirstObject();
         if(obj)
         {
              GetAllCameras(oCams,obj);
         }

    return oCams->GetCount();
    }

    void SnapShoot(String CamName)
    {
         BaseDocument *doc=GetActiveDocument();
         BaseObject *obj=doc->SearchObject(CamName);
         doc->GetRenderBaseDraw()->SetSceneCamera(obj);
    StartEditorRender(FALSE,TRUE,0,0,500,500,NULL,doc->GetActiveBaseDraw(),FALSE);
    }

    If I'm calling the first two functions from the click event of a button ... like below ...

    AtomArray *oCams=AtomArray::Alloc();
                   LONG lCamCnt=GetAllCameras(oCams);
                   MessageDialog(LongToString(lCamCnt));
                   for(int i=0;i<lCamCnt;i++)
                   {
                        MessageDialog(((BaseObject* )oCams->GetIndex(i))->GetName());
                   }

    return TRUE;

    It's working ...
    So it shows the first two functions are working ...
    So let's change the code in the click event to this ...

    AtomArray *oCams=AtomArray::Alloc();
                   LONG lCamCnt=GetAllCameras(oCams);
                   MessageDialog(LongToString(lCamCnt));
                   for(int i=0;i<lCamCnt;i++)
                   {
                        SnapShoot(((BaseObject* )oCams->GetIndex(i))->GetName());
                   }

    return TRUE;

    SnapShoot is a function just for rendering a 0,0 to 500,500 of the document ...
    Then it may crash if you use 0,0 to 1000,1000 or so ... but not exactly we can say ... even 500,500 is crashing ... reduce it ... :)
    So what I doubt is the error may come from the Rendering function ...

    Wishing you make it accomplish ...
    Zaw Min Tun


Log in to reply