Posts tagged crash

Function call by using null pointer will crash?

3


Crash on function call by using null pointer – The ultimate C++ Nirvana. ;) Whether function call by null pointer always crash? Is it possible to call functions by using null pointer without crash? Check the following code snippet. Whether it will crash?

// Console Helper class.
class ConsoleHelper
{
public:
    // Prints a line to console.
    void PrintLine()
    {
        cout << "---------------" << endl;
    }
};

// The one and only one Main.
int main(int argc, char* argv[])
{
    // Call the function by using null pointer.
    ConsoleHelper* pConsoleHelper = 0;
    pConsoleHelper->PrintLine();

    return 0;
}


It won’t crash. Because in the function call we’re not accessing any member variables. Function call by using null pointers crash only if we’re accessing some member variables. Its because for every object function call, the object address is pushed to the function stack as “this” pointer. If the object pointer is null, then the “this” pointer also become null and hence the crash!


Null pointers are not that much bad! isn’t it? ;)


Targeted Audience – Intermediate.

auto_ptr as pass by value to functions – you are going to face a crash.

0


Memory leaks are our nightmare and auto_ptr’s are used for making our life easier which does automatic memory deletion. If a pointer is assigned to an auto_ptr, it will be deleted when the auto_ptr went out of scope. But use this automatic deletion carefully, especially while passing it as function arguments by value. You are going to face a crash!


The problem is “owership”. Every auto_ptr holds an ownership flag. when an auto_ptr is assigned to another auto_ptr, the ownership flag is transfered from first to second. And only the auto_ptr with ownership flag enabled, can delete the resource. This method is used to avoid multiple deletion of the same pointer kept by several auto_ptrs. There will be only one owner auto_ptr at a time.

Now when you pass the auto_ptr by value, actually, you are passing the owner ship to the stack variable of the called function. Now the stack parameter will become the owner of your pointer and when the function returns, the auto_ptr in stack will be destructed and since its the owner, it will delete your passed pointer. When you access you main auto_ptr, then you’ll probably get a crash!!! See the code snippet for an e.g.

// Function which receives auto_ptr as value.
void ToUpper( auto_ptr<CString> pString )
{
    // Shows the messageBox.
    pString->MakeUpper();
}

// The one and only Main.
void main()
{
    // An auto_ptr which holds a CString.
    auto_ptr<CString> pString(
        new CString(_T("Hello AutoPtr")));

    // Make the string upper case by
    // passing it as value.
    ToUpper( pString );

    // At this point, your CString is already
    // been deleted by the stack variable in function.

    // Now change the string to Lower and face the crash!!!
    pString->MakeLower();
}


You can make the situation safe by passing by reference. While doing so, only a reference is passed to the stack variable and hence the ownership remains in you main auto_ptr itself.


Targeted Audience – Intermediate.

List the loaded modules in your crash report.

1


In crash reports, usually we see a list of loaded modules. While writing Error Handler frameworks, for dumping crash reports we too need to get list of loaded modules which will make the debugging tasks easier. But how can we find out the loaded modules? You can use EnumerateLoadedModules() function to enumerate the loaded modules.


You can use EnumerateLoadedModules() function to enumerate the loaded modules. For calling EnumerateLoadedModules() we need provided a callback function pointer. The EnumerateLoadedModules() will send the loaded module information as callback to that provided function. Please see the following code

#include <Dbghelp.h>

// Callback function.
BOOL CALLBACK EnumerateLoadedModulesProc( PSTR ModuleName,
                                          ULONG ModuleBase,
                                          ULONG ModuleSize,
                                          PVOID UserContext )
{
    // Print the module name.
    cout << ModuleName << endl;
    return TRUE;
}

int main(int argc, char* argv[])
{
    // Enumerate loaded modules.
    EnumerateLoadedModules(
       GetCurrentProcess(),	// Process Handle
       // Callback function pointer
       (PENUMLOADED_MODULES_CALLBACK) EnumerateLoadedModulesProc,
       0 );	// User context.

       return 0;
}

Add beauty and complexity to your crash report by listing out the module names ;)

Go to Top