CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2009
    Posts
    166

    warning C4715: 'CM::Instance' : not all control paths return a value

    Hello, I have the following code where I use the singleton design pattern, but I get the warning:

    warning C4715: 'CM::Instance' : not all control paths return a value

    Code:
    CM& CM::Instance()
    {	
        DWORD dwWaitResult = WaitForSingleObject(mutex, INFINITE);
        switch(dwWaitResult)
        {
            case WAIT_OBJECT_0:
           {
                 static CM& inst = CM();
                 ReleaseMutex(mutex);
                 return inst;
           }
       }
       return CM();
    }
    How can I fix this warning?

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: warning C4715: 'CM::Instance' : not all control paths return a value

    Quote Originally Posted by ekhule View Post
    Hello, I have the following code where I use the singleton design pattern,
    And what would happen if that switch statement never executes? You are then returning a reference to a locally created object, which is undefined behaviour.

    You need to fix your code first, and forget about the warning.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Mar 2009
    Posts
    166

    Re: warning C4715: 'CM::Instance' : not all control paths return a value

    How would you fix this code? I am stumped!

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: warning C4715: 'CM::Instance' : not all control paths return a value

    What do you want to return if WaitForObject signals WAIT_ABANDONED or WAIT_FAILED? As you are returning a reference to an object, that object MUST exist once the method exits - even in error situations. How does the code calling this method know why the WaitForSingleObject was signalled? You really have only a choice of three options - with variations.

    1) return an error code as a ref parameter to Instance (eg DWORD& code) and always return a ref to the static CM and let the calling program test for error or not.

    2) if signal is not WAIT_OBJECT_0 then handle the error within the method, re-signal the mutex and wait again for the signal so that when Instance returns it always returns when WAIT_OBJECT_0 is signalled. This option may not be possible within your design.

    3) Pass CM& as a parameter to Instance rather than a return value, and return a DWORD indicating reason for signal. Again, this will require changes to how this method is called.

    What is best in your cirumstances depend upon how much control you have to re-defining the defintion of the Instance method.

  5. #5
    Join Date
    Oct 2008
    Posts
    1,456

    Re: warning C4715: 'CM::Instance' : not all control paths return a value

    BTW, that code is illegal as you cannot return an lvalue reference to a temporary, so your code should not compile ( it does just because you're using a VC language extension ). Second, that warning seems wrong too as the switch should pass control to the last return statement when the wait result doesn't match ...

    anyway, another possibility could be

    4) to use a RAII syncronization object and throw an exception on failure
    5) to return a pointer to CM ( being NULL in case of error ) or boost:: optional<CM&> if for some reason the Instance method is expected to not return a valid instance even under "normal" circumstances ...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured