Weird behavior isolated?

Hi there i’m knocking my head again the wall for two days now trying to isolate this glitch in my code… but i got a nice picture lol !
i pick 3d CV point on a surface move those point 3 times fine every thing work has intended,
finally i put this code in a function for avoiding repeating lines of code and then the function
break after one success move and write 2 more glitched point (the two remaining) in the document
(the first one is write where it expected to be) you can see the two other (glitch ghosted) if you rectangle selection at the location…

@Dale if you can help me on this that would be awesome it’s just those details where everything
is just not perfect as it should that turn me mad…
you may have quick explanation for that what i’m doing wrong?

void Mv(CRhinoObject* GripsOwner, CRhinoObject* newObj, int CVindex, ON_3dPoint destination, const CRhinoCommandContext& context) {
	GripsOwner->EnableGrips(1);
	GripsOwner->m_grips->m_grip_list[CVindex]->MoveGrip(destination);
	newObj = GripsOwner->m_grips->m_grip_list[CVindex]->m_grip_owner->NewObject();
	context.m_doc.ReplaceObject(CRhinoObjRef(GripsOwner), newObj, true);
	GripsOwner = newObj;
	context.m_doc.Redraw();
}

CRhinoCommand::result CCommandaTest18::RunCommand(const CRhinoCommandContext& context)
{
	CRhinoGetObject go;
	go.SetGeometryFilter(CRhinoGetObject::grip_object);
	go.SetCommandPrompt(L"Select Ctrlpt:");
	go.GetObjects(3, 3);

	if (go.CommandResult() == CRhinoCommand::cancel) {
		return cancel;
	}

	int count = go.ObjectCount();
	ON_wString msg;
	msg.Format(L"----->%d\r\n", count);
	RhinoApp().Print(msg);
	CRhinoGripObject* GripList = new CRhinoGripObject[count];
	CRhinoObjRef* GripRef = new CRhinoObjRef[count];
	ON_3dPoint* ptlist = new ON_3dPoint[count];
	int* CVindex = new int[count];
	CRhinoObject* newObj = nullptr;

	for (int i = 0; i < count; i++)
	{
		GripRef[i] = go.Object(i);
		CRhinoObject* obj = const_cast<CRhinoObject*> (go.Object(i).Object());
		GripList[i] = *CRhinoGripObject::Cast(obj); // Copy a real rhino Object ivalue Grips.
		ptlist[i] = GripList[i].m_base_point;       // *1 collect data point base point.
		CVindex[i] = GripList[i].m_grip_index;      // *2 collect index.   
	}

	CRhinoObject* GripsOwner = GripList[0].Owner();
	GripsOwner->EnableGrips(1);

	for (int i = 0; i < count; i++)
	{
		GripsOwner->m_grips->m_grip_list[CVindex[i]]->MoveGrip(ON_3dPoint(5, 5, 5 + i));
		newObj = GripsOwner->m_grips->m_grip_list[CVindex[i]]->m_grip_owner->NewObject();
		context.m_doc.ReplaceObject(CRhinoObjRef(GripsOwner), newObj, true);
		GripsOwner = newObj;
	}
	context.m_doc.Redraw();

	if (GripsOwner) {
		RhinoApp().Print("Object is ok.");
	}

	for (int i = 0; i < count; i++)
	{
		GripsOwner->m_grips->m_grip_list[CVindex[i]]->MoveGrip(ON_3dPoint(10, 10, 5 + i));
		newObj = GripsOwner->m_grips->m_grip_list[CVindex[i]]->m_grip_owner->NewObject();
		context.m_doc.ReplaceObject(CRhinoObjRef(GripsOwner), newObj, true);
		GripsOwner = newObj;
	}

	context.m_doc.Redraw();
	if (GripsOwner) {
		RhinoApp().Print("Object is ok Step2");
	}

	for (int i = 0; i < count; i++)
	{
		GripsOwner->m_grips->m_grip_list[CVindex[i]]->MoveGrip(ON_3dPoint(15, 15, 5 + i));
		newObj = GripsOwner->m_grips->m_grip_list[CVindex[i]]->m_grip_owner->NewObject();
		context.m_doc.ReplaceObject(CRhinoObjRef(GripsOwner), newObj, true);
		GripsOwner = newObj;
	}
	context.m_doc.Redraw();

	if (GripsOwner) {
		RhinoApp().Print("Object is ok Step3");
	}

	for (int i = 0; i < count; i++)
	{
		Mv(GripsOwner, newObj, CVindex[i], ON_3dPoint(20, 20, 5 + i), context);
		if (GripsOwner) {
			RhinoApp().Print("Object is ghosted in step 4 ghost point are present in doc at ON_3dPoint(20, 20, 5 + i)");
		}
	}

	return CRhinoCommand::success;
}

Hi @JulienPoivret,

If you want to move grips, then please review the following sample and let me know if you have any questions.

cmdSampleMoveGrips.cpp

– Dale

hey @Dale thank you for answering but my question is not about moving Grip its about why my code pattern can be repeated in the RunCommand and not in a simple function i cannot see any reason ? and thank you again to stay in touch :grinning:

Hi @JulienPoivret,

Looks to me like you’re moving grips. Perhaps I’m confused.

Did you have a look at the sample I referenced?

I should add that your code is not of good quality. If you’re going to distribute this tool, you should always validate pointers before dereferencing.

– Dale

hi @Dale you don’t get it, that code is not a plugin code and even less a release one… i’m far from there… that just a minimalist working example snaping my issue…
im just struggling to set a working code of the MyCommand::RunCommand() method (like above) working from a function call :grinning:
( and validating a null pointer doesn’t make it valid any way but rather complexify the show case)
in my show case there are 3 working patterns the 4th one (from a function call is not working be cause of course of a null pointer dereferencing but just why there and not before you see ? no reason for that at least one that i can understand and it’s where i need help… :crossed_fingers: )
all my hope all my best !

hi, i have put some effort to simplify a WORKING code in the RunCommand() call a usual…
but when i implement this in the most basic way,
in a simple function that just fail for no obvious reason (i got null ptr where it should not…)
i guess it’s for the benefit of all of us to take a look in such basic weird behavior
maybe it’s me, but may be the API may also need some maintenance too ? who know !? :grinning:
further more c++ culture is more than use full in 3d computing related so i would love to have
more topic in this forum related to C++ for the benefit of all the community.
After all for involved people with passion,
it’s just about knowing how to use computer the (memory at least) that my point every thing have to be maintain with passion and respect for what they are. C is not so popular on this forum it’s a mistake… some have time for this (like me) so let have some fun no ? so come on people come under the hood ! :smiley:).

hope that @Dale will not be too upset he do his best for helping me at each time, special thanks to him !
here the code:

void Mv(CRhinoObject* GripsOwner, CRhinoObject* newObj, int CVindex, ON_3dPoint destination, const CRhinoCommandContext& context) {
	RhinoApp().Print(L"function entrance\r\n");
	if (GripsOwner->GripsOn() == false) {
		RhinoApp().Print(L"Gip was Off\r\n");
		GripsOwner->EnableGrips(1);
		GripsOwner->m_grips->SelectGrips(true, true, true);
		int index;
		if (index = GripsOwner->m_grips->m_grip_list[CVindex]->Select() > 0) {
			RhinoApp().Print(L"a Gip was Selected.\r\n");
			if (index == 2) {
				RhinoApp().Print(L"Grip is Persistent\r\n");
			}
			else if (index == 1) {
				RhinoApp().Print(L"a Grip is Selected in Normal mode\r\n");
			}
		}
	}
	if (GripsOwner->m_grips->m_grip_list[CVindex]) {
		GripsOwner->m_grips->m_grip_list[CVindex]->MoveGrip(destination);
		bool D = true;
		if (D == true) {
			if (GripsOwner = RhinoUpdateGripOwner(GripsOwner, false, nullptr)) {
				RhinoApp().Print(L"Replacement is a success from the function.\r\n");
			}
			else {
				RhinoApp().Print(L"Bug with this new owner replacement...\r\n");
			}
		}
		else {
			if (newObj = GripsOwner->m_grips->m_grip_list[CVindex]->m_grip_owner->NewObject()) {
				RhinoApp().Print(L"New owner is valid\r\n");
				if (context.m_doc.ReplaceObject(CRhinoObjRef(GripsOwner), newObj, true)) {
					GripsOwner = newObj;
					context.m_doc.Redraw();
					RhinoApp().Print(L"Replacement is a success from the function.\r\n");
				}
				else {
					RhinoApp().Print(L"Bug with this new owner replacement...\r\n");
				}
			}
			else {
				RhinoApp().Print(L"New owner is not valid\r\n");
			}
		}
	}
	else {
		RhinoApp().Print(L"No Grip at that index.\r\n");
	}
}

CRhinoCommand::result CCommandtest::RunCommand(const CRhinoCommandContext& context)
{
	CRhinoGetObject go;
	go.SetGeometryFilter(CRhinoGetObject::grip_object);
	go.SetCommandPrompt(L"Select Ctrlpt:");
	go.GetObjects(3, 3);

	if (go.CommandResult() == CRhinoCommand::cancel) {
		return cancel;
	}

	int count = go.ObjectCount();
	ON_wString msg;
	msg.Format(L"----->%d\r\n", count);
	RhinoApp().Print(msg);
	CRhinoGripObject* GripList = new CRhinoGripObject[count];
	CRhinoObjRef* GripRef = new CRhinoObjRef[count];
	ON_3dPoint* ptlist = new ON_3dPoint[count];
	int* CVindex = new int[count];
	CRhinoObject* newObj = nullptr;

	for (int i = 0; i < count; i++)
	{
		GripRef[i] = go.Object(i);
		CRhinoObject* obj = const_cast<CRhinoObject*> (go.Object(i).Object());
		GripList[i] = *CRhinoGripObject::Cast(obj); // Copy a real rhino Object ivalue Grips.
		ptlist[i] = GripList[i].m_base_point;       // *1 collect data point base point.
		CVindex[i] = GripList[i].m_grip_index;      // *2 collect index.   
	}

	CRhinoObject* GripsOwner = GripList[0].Owner();
	GripsOwner->EnableGrips(1);
	GripsOwner->m_grips->SelectGrips(true, true, true);

	for (int i = 0; i < count; i++)
	{
		GripsOwner->m_grips->m_grip_list[CVindex[i]]->MoveGrip(ON_3dPoint(5, 5, 5 + i));
		newObj = GripsOwner->m_grips->m_grip_list[CVindex[i]]->m_grip_owner->NewObject();
		context.m_doc.ReplaceObject(CRhinoObjRef(GripsOwner), newObj, true);
		GripsOwner = newObj;
	}
	context.m_doc.Redraw();

	if (GripsOwner) {
		RhinoApp().Print("Object is ok Step1\r\n");
	}

	for (int i = 0; i < count; i++)
	{
		GripsOwner->m_grips->m_grip_list[CVindex[i]]->MoveGrip(ON_3dPoint(10, 10, 5 + i));
		newObj = GripsOwner->m_grips->m_grip_list[CVindex[i]]->m_grip_owner->NewObject();
		context.m_doc.ReplaceObject(CRhinoObjRef(GripsOwner), newObj, true);
		GripsOwner = newObj;
	}
	context.m_doc.Redraw();

	if (GripsOwner) {
		RhinoApp().Print("Object is ok Step2\r\n");
	}

	for (int i = 0; i < count; i++)
	{
		GripsOwner->m_grips->m_grip_list[CVindex[i]]->MoveGrip(ON_3dPoint(15, 15, 5 + i));
		newObj = GripsOwner->m_grips->m_grip_list[CVindex[i]]->m_grip_owner->NewObject();
		context.m_doc.ReplaceObject(CRhinoObjRef(GripsOwner), newObj, true);
		GripsOwner = newObj;
	}
	context.m_doc.Redraw();

	bool withbug = true;
	if (withbug==true) {
		if (GripsOwner) {
			RhinoApp().Print("Object is ok Step3\r\n");
			for (int i = 0; i < count; i++)
			{
				Mv(GripsOwner, newObj, CVindex[i], ON_3dPoint(20, 20, 5 + i), context);
			}
		}
	}
	if (GripsOwner) {
		RhinoApp().Print("GripOwner Stil Valid.");
		GripsOwner->EnableGrips(0);
	}
	return CRhinoCommand::success;
}