Error 4051 occurred deleting an object

Back to topics list To post a new topic, please log in or register
peterhw1 2015.09.24 16:25

I use a routine to create an entire table as a summary of all my active trades and each object has a name created by using its x and y positions.

The table maybe 10 columns by 10 lines - i.e. 100 objects created in the same routine -- but all very similar

I noticed the following error 4051 and created some additional 'FileWrite' statements to try and track the error.

It is only COD330_130 that appears to generate this problem. 

The next line would be - say - COD345_130 and no problem

There don't appear to be any other problems and the EA continues to work


        string st_44444 = " ObjName = "+ObjName+", Object_label = "+Object_label+" colour = "+IntegerToString(colour)+
         " xd = "+IntegerToString(xd)+" yd = "+IntegerToString(yd)+", ";
   if(ObjectFind(ObjName)>-1) ObjectDelete(ObjName);
   My_error=GetLastError(); if(My_error>0) FileWrite(My_Handle,"1"+st_44444+IntegerToString(My_error)+" "+ErrorDescription(My_error));

   The 'FileWrite' generated the following

   1 ObjName = COD330_130, Object_label = EURCHF colour = clrMidnightBlue xd = 330 yd = 130, 4051 invalid function parameter value

Any ideas appreciated.

jjc 2015.09.24 19:27 #

[...] Any ideas appreciated.

Are you sure that the error 4051 is generated during the code which you have posted, rather than before it?

It seems far more likely that the sequence of events, and the issue, is as follows:

  • Block of code earlier to that which you have posted, which does something leading to error 4051
  • Then the code which you have posted, which doesn't generate an error
  • Checking GetLastError() then gives you the earlier error, which happened before the block of code which you are querying here. 
If you haven't already done so, try doing a (dummy) call to GetLastError immediately before the object deletion. That will retrieve and discard the most recent error code. If the use of GetLastError immediately after the object deletion still returns 4051, then you do know that it's actually being caused by the object deletion.

jjc 2015.09.24 19:31 #

Are you sure that the error 4051 is generated during the code which you have posted, rather than before it?


To put that another, briefer way:

   // The following fails...
   ObjectDelete("any invalid object name which doesn't exist"); // Fails
   // The following succeeds...
   ObjectCreate(0, "new object", OBJ_LABEL, 0, 0, 0); // Succeeds
   // The following returns 4202, because the error from the use of ObjectDelete()
   // isn't cleared by the subsequent successful ObjectCreate()
   int err = GetLastError(); // Returns 4202


jjc 2015.09.24 19:42 #

To put that another, briefer way: [...]

... or, more generally, use of GetLastError() is meaningless "in isolation". You should only use GetLastError() under one of the following circumstances:

A. You know that GetLastError() is applicable to the most recent action.

For example:

if (OrderSend(...) < 0) {
  // Order-send has failed
  // The MT4 documentation guarantees that GetLastError() now contains the reason for the failure of this most recent action


B. You deliberately reset GetLastError() before carrying out an action which you want to test

For example:

GetLastError(); // Dummy call which gets, discards, and clears the last-error, setting it to zero

... some action which you think may generate an error ...

// Use of GetLastError() now tells you whether an error occurred since the time
// when you explicitly cleared it, or returns 0 if no error occurred in that block of code
int err = GetLastError();



peterhw1 2015.09.25 11:01 #

Many thanks to everyone. 

I have placed a GetLastError before the statement in question - and correct the error exists prior to this statement.

I am now trying to track the appropriate code.

Back to topics list  

To add comments, please log in or register