C++ API Reference: crash/crashAction.cpp

crash/crashAction.cpp
// File: crashAction.cpp
//
// MEL Command: crashAction
//
// Command to crash Maya in known ways for testing purposes
//
#include <maya/MPxCommand.h>
#include <maya/MGlobal.h>
#include <maya/MArgList.h>
#include <maya/MGlobal.h>
#include <maya/MString.h>
#include <maya/MFnPlugin.h>
#include <math.h>
#include <stdio.h>
#include <assert.h>
#include <maya/MApiNamespace.h>
// Help strings
//
static const char *helpArray[] = {
"Synopsis : crash [flags]\n",
" -a/allocation : Crash through excessive memory allocation\n",
" -m/melCommand : A magic Mel script syntax error that crashes the command engine\n",
" -n/nullPointer : Illegal memory access through a zero pointer\n"
};
class crashAction : public MPxCommand
{
public:
crashAction() {}
~crashAction() override {}
MStatus doIt(const MArgList &) override;
bool isUndoable() const override { return false; }
static void *creator() { return new crashAction(); }
};
//======================================================================
//
MStatus crashAction::doIt(const MArgList &args)
{
MStatus status = MS::kFailure;
// Keep track of whether anything was done so that help information can be
// printed if nothing legal was specified.
bool didSomething = false;
for (unsigned i = 0; i < args.length(); i++)
{
// Check for type of crash requested. Default is to show help
// information since this command isn't part of the normal Maya
// help system.
//
if (((MString("-a") == args.asString(i, &status)) || (MString("-allocation") == args.asString(i, &status))) && (MS::kSuccess == status))
{
for (unsigned int i = 0; i < 0xffffffff; ++i)
{
// Allocate without freeing and watch the fireworks
(void)malloc(0xfffffffffffffff);
}
didSomething = true;
}
else if (((MString("-m") == args.asString(i, &status)) || (MString("-melCommand") == args.asString(i, &status))) && (MS::kSuccess == status))
{
MString command("global proc crashMe(, int $test ) {}");
didSomething = true;
}
else if (((MString("-n") == args.asString(i, &status)) || (MString("-nullPointer") == args.asString(i, &status))) && (MS::kSuccess == status))
{
MString *nullStr = NULL;
printf("Printing an invalid value %s\n", nullStr->asChar());
didSomething = true;
}
}
if (!didSomething)
{
unsigned int helpCount = sizeof(helpArray) / sizeof(helpArray[0]);
for (unsigned int j = 0; j < helpCount; j++)
{
appendToResult(MString(helpArray[j]));
}
return MS::kSuccess;
}
// What happened to the Kaboom?
// There was supposed to be an Earth-shattering Kaboom?
//
return MS::kFailure;
}
MStatus initializePlugin(MObject obj)
//
// Description:
// this method is called when the plug-in is loaded into Maya. It
// registers all of the services that this plug-in provides with
// Maya.
//
// Arguments:
// obj - a handle to the plug-in object (use MFnPlugin to access it)
//
{
MStatus status;
MFnPlugin plugin(obj, "Autodesk", "4.0", "Any");
status = plugin.registerCommand("crash", crashAction::creator);
if (!status)
{
status.perror("registerCommand");
return status;
}
return status;
}
MStatus uninitializePlugin(MObject obj)
//
// Description:
// this method is called when the plug-in is unloaded from Maya. It
// deregisters all of the services that it was providing.
//
// Arguments:
// obj - a handle to the plug-in object (use MFnPlugin to access it)
//
{
MStatus status;
MFnPlugin plugin(obj);
status = plugin.deregisterCommand("crash");
if (!status)
{
status.perror("deregisterCommand");
return status;
}
return status;
}
//-
// ==================================================================
// Copyright 2020 Autodesk, Inc. All rights reserved.
//
// This computer source code and related instructions and comments are
// the unpublished confidential and proprietary information of Autodesk,
// Inc. and are protected under applicable copyright and trade secret
// law. They may not be disclosed to, copied or used by any third party
// without the prior written consent of Autodesk, Inc.
// ==================================================================
//+