Crash in Paint(g.DrawRect…)

I cannot understand how that can be related…

I had a bug in my Preferences (Open or Save).

Today was the day to squash it.
So I decided to start by comparing the Save and Open Preferences Methods.
I moved the code of Open to start displaying at the first Write, do the same for the Save and wanted to find an error alterning the views (same monitor).
Too bad, Xojo move the display of the first line of code to the top of the Method !

OK: second guess; I will have to try to remember what I read from one method to the other, and after a while (I was even thinking to write the sequence n a paper shee), I discovered the error (I saved two different colors using only one in two lines… look I do not even explain it correctly !). But the new save was correct.

I run the project and get a crash: apparently a declare I use do not xists in El Capitan. Read the onlne docs, found “IsFunctionAvailable”, add it, run: everything seems OK.
Quit (to write the correct Preferences sequence).
Run and get the Paint Event crash.

OK: left El Capitan, fired the external SSD with High Sierra, fire my Xojo, run and crash:

And now I get a NilObjectException in the Paint Event:
g.DrawRect… !

Left Xojo 2015r1, fire the current version, load the project, run and get the NilObjectExcepton at the same location.

Comment the whole declare block, run: same crash.

Why, the current Xojo IDE window, at open time is so small ?

Time ellapsed.

I read the Prefeerences file, gave smiles to a baby, look around, and when I read my Preferences file contents, noticed a value (the last entry) is way beyond control ! It have to be less that 10. I commented these lines, run… once, twice, thrice…

Now the question:
The problem lie in the value of a control set of Canvases (the Canvas Member index):


How can I get the number of Canvases in the set ?
With that number, I can add a test and set the value to a defaut if it is out of range…

The DrawRect line that crash draw a rectangle around each Canvas Members, and they are (currently) 5, not thousand(s)…

Off by one?

To me it looks like g is nil. (And yes, it is debatable if this should happen).
How about a
If g <> nil then …
to include everything else in the paint event?

In fact, in the Paint Event, I draw a Focus Ring around a Country Flag as a user feedback on the selected language.

The used Canvas are Member of a Control Set, so I only have to write the draw code once.

That Control Set Index s saved in Preferences and was the faultive due to a bug introduced in the last changes session.

Faultive code:

  g.DrawRect cFlag(mGraphics.gMonth_Langue).Left - 4,cFlag(mGraphics.gMonth_Langue).Top - 4,_
  cFlag(mGraphics.gMonth_Langue).Width + 8,cFlag(mGraphics.gMonth_Langue).Height + 8

At the first look, how can g.DrawRect can lead to a NilObjectException ?

cFlag is the name of the Canvas Control Set
mGraphics.gMonth_Langue is an Integer uset to read/write the string in one of the 5 available languages. *

It was a CSB: Cascade Sheet Bug !

Since then, I had some coding sessions without incident.
I even wrote a wrd vertically in black above a red background in less than 5 minutes…
Rarely, but sometimes everything goes allright.

Have a nice week-end/end of month everybody.

  • Maybe, if I stored the Language Integer (mGraphics.gMonth_Langue) into a local variable (named Index), the debug session would be different ?
    Less text, easer to understand code.

i would cubclass this canvas and add a computed property Selected (as memory and set .Invalidate)
so it can draw itself as selected in paint event.
at a click event all get unselected and the sender get selected.

then it looks like

if Selected then
g.DrawRect 0,0,g.Width,g.Height

There are 5 canvas (each hold a different Flag / Country) that represent a Language (German, English, French, Italian and Spanish).
That is why I set them as a Control Set (a premiere for me).

The new code (below) is in the Window’s Paint Event:

g.DrawRect cFlag(Index).Left - 4,cFlag(Index).Top - 4, cFlag(Index).Width + 8,cFlag(Index).Height + 8

The ForeColor is set to HighlightColor and the pen is set to 4 (pixels).

A screen shot of this window is used in another thread about OS Tabbed Windows…

A Click in one of these Canvas change the display language and set the Focus to that Canvas.

Kein Fett in diesem Code.

1 Like