Thread: Method returning NULL fails?

  1. #1
    Registered User
    Join Date
    Nov 2006
    Location
    japan
    Posts
    126

    Question Method returning NULL fails?

    Hi,
    I have the following method and I would like to know why this makes my program to crash?
    I come from an Objective-C background where nothing occurs when an instance that is NULL or zero is sent a method.
    Code:
    blob LLAHTracking::ClosestBlobToPoint2D(CvPoint point){
    	
    	int minDist = 999999;
    	int dist;
    	blob *closestBlob = NULL;
    	MyBlobList::blobs *blobs = m_bloblist.ExtractedBlobs();
    
    	for(MyBlobList::blobs::iterator iblob = (*blobs).begin(); iblob != (*blobs).end(); iblob++){
    		dist = (*iblob)->distance(point.x, point.y);
    		if( dist < minDist ){
    			minDist = dist;
    			closestBlob = (*iblob);
    		}
    	}
    	return *closestBlob;
    }
    As you can see, when there is no blobs to iterate in the list the method will return NULL and my program just crashes.
    How can I solve this kind of errors? Or maybe is any other reason I might be missing?

    Thanks in Advance
    Mac OS 10.6 Snow Leopard : Darwin

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by nacho4d View Post
    I come from an Objective-C background where nothing occurs when an instance that is NULL or zero is sent a method.
    Okay, but if data that is essential to some process is missing, there is no possible language where that will NOT result in a (probably fatal) error.

    In this case, if it is "point" you are referring to:
    Code:
    		dist = (*iblob)->distance(point.x, point.y);
    If point is NULL it has no members x and y.

    If you mean blobs, the same logic applies. If that is a possible outcome (m_bloblist.ExtractedBlobs() could return NULL), you need to check the return value.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    You don't return NULL. You return NULL, dereferenced. Returning NULL (being a pointer) is fine.

    However, how it works now, is that you return something that is copied (blob), as you return not the pointer but the object itself. Now when this object is copied, it's copied from the memory address 0 to another place where the return value is stored. But this address 0 can't be read, in your case, crashing the program.


    To return NULL, return a POINTER. The pointer will be NULL; that's fine. But don't -ever- dereference NULL.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Location
    japan
    Posts
    126

    Talking

    Quote Originally Posted by EVOEx View Post
    You don't return NULL. You return NULL, dereferenced. Returning NULL (being a pointer) is fine.

    However, how it works now, is that you return something that is copied (blob), as you return not the pointer but the object itself. Now when this object is copied, it's copied from the memory address 0 to another place where the return value is stored. But this address 0 can't be read, in your case, crashing the program.


    To return NULL, return a POINTER. The pointer will be NULL; that's fine. But don't -ever- dereference NULL.
    Thanks! that was the problem, I change my method from this
    blob LLAHTracking::ClosestBlobToPoint2D(CvPoint point);
    to this
    blob * LLAHTracking::ClosestBlobToPoint2D(CvPoint point);
    also I am checking blobs is not NULL before using it. And now It runs fine.


    MK27:
    Okay, but if data that is essential to some process is missing, there is no possible language where that will NOT result in a (probably fatal) error.
    Yeah, you are right, if you pass a NULL or equivalent as the argument of a method it might make a program to crash, but if you do for example:
    Code:
    Object *obj = nil;  //equivalent of NULL in Objective-C 
    obj.something;
    in Objective-C it will run without problems (it does nothing ) and this is a big difference with C++ that causes me trouble sometimes
    Anyways. Thanks for your help guys.

    You saved my day
    Last edited by nacho4d; 05-27-2010 at 11:39 AM.
    Mac OS 10.6 Snow Leopard : Darwin

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "Virtual Printer" or "Moving Printjobs"
    By extasic in forum Windows Programming
    Replies: 12
    Last Post: 06-30-2011, 08:33 AM
  2. IDE for C embedded advanced editing
    By Undici77 in forum Tech Board
    Replies: 32
    Last Post: 01-16-2010, 05:17 PM
  3. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 05:48 PM
  4. Memory leaks problem in C -- Help please
    By Amely in forum C Programming
    Replies: 14
    Last Post: 05-21-2008, 11:16 AM
  5. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM