473,405 Members | 2,154 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

How to check if a pointer is "valid" ?

I think I can't find this on google/books because is soooooooo basic...

This function assign a listner pointer to an image widget:
------------------------------------------------------
void image::setListener(AbstractListener *listener)
{
this->listener = listener;
}
------------------------------------------------------
this function use the listner:

------------------------------------------------------
bool image::isMouseOver(GLint mouseX, GLint mouseY)
{
bool isOver = false;
if (mouseX > positionX
&& mouseX < positionX + width
&& mouseY > positionY
&& mouseY < positionY + height)
{
listener->mouseOver(mouseX, mouseY, userValue);
isOver = true;
}
return isOver;
};
------------------------------------------------------

Of course, If I call the function above, without call setListner before,
the application crash.

So I must check listner is OK.
I'm trying somewhat like:

------------------------------------------------------
bool image::isMouseOver(GLint mouseX, GLint mouseY)
{
bool isOver = false;
if (mouseX > positionX
&& mouseX < positionX + width
&& mouseY > positionY
&& mouseY < positionY + height)
{
if (listener)
{
listener->mouseOver(mouseX, mouseY, userValue);
}
isOver = true;
}
return isOver;
};
------------------------------------------------------

But it crash anyway...

Please, help...

Regards,

Manuel
Jan 9 '06 #1
4 12109
Manuel wrote:
I think I can't find this on google/books because is soooooooo basic...

This function assign a listner pointer to an image widget:
------------------------------------------------------
void image::setListener(AbstractListener *listener)
{
this->listener = listener;
}
------------------------------------------------------
this function use the listner:

------------------------------------------------------
bool image::isMouseOver(GLint mouseX, GLint mouseY)
{ [..]
listener->mouseOver(mouseX, mouseY, userValue);
[..]
};
------------------------------------------------------

Of course, If I call the function above, without call setListner before,
the application crash.

So I must check listner is OK.
Simple. Initialise 'listener' with 0 in the constructor. Check for its
value before using it:

if (listener)
listener->mouseOver(...
I'm trying somewhat like:

------------------------------------------------------
bool image::isMouseOver(GLint mouseX, GLint mouseY)
{
bool isOver = false;
if (mouseX > positionX
&& mouseX < positionX + width
&& mouseY > positionY
&& mouseY < positionY + height)
{
if (listener)
{
listener->mouseOver(mouseX, mouseY, userValue);
}
isOver = true;
}
return isOver;
};
------------------------------------------------------

But it crash anyway...


Not if you actually initialise 'listener' to 0. Otherwise, it's left
uninitialised, and using an uninitialised pointer causes undefined
behaviour.

V
Jan 9 '06 #2

"Manuel" <ma**********************@tin.it> wrote in message
news:43**********************@reader4.news.tin.it. ..
I think I can't find this on google/books because is soooooooo basic...

This function assign a listner pointer to an image widget:
------------------------------------------------------
void image::setListener(AbstractListener *listener)
{
this->listener = listener;
}
------------------------------------------------------
this function use the listner:

------------------------------------------------------
bool image::isMouseOver(GLint mouseX, GLint mouseY)
{
bool isOver = false;
if (mouseX > positionX
&& mouseX < positionX + width
&& mouseY > positionY
&& mouseY < positionY + height)
{
listener->mouseOver(mouseX, mouseY, userValue);
isOver = true;
}
return isOver;
};
------------------------------------------------------

Of course, If I call the function above, without call setListner before,
the application crash.

So I must check listner is OK.
I'm trying somewhat like:

------------------------------------------------------
bool image::isMouseOver(GLint mouseX, GLint mouseY)
{
bool isOver = false;
if (mouseX > positionX
&& mouseX < positionX + width
&& mouseY > positionY
&& mouseY < positionY + height)
{
if (listener)
{
listener->mouseOver(mouseX, mouseY, userValue);
}
isOver = true;
}
return isOver;
};
------------------------------------------------------

But it crash anyway... one cannot check to see if a pointer is valid or not. Not in standard C++.

Please, help... in the constructor of image object, set listener pointer to zero.


Regards,

Manuel

Jan 9 '06 #3
Manuel wrote:
I think I can't find this on google/books because is soooooooo basic...
No, you can't find this on Google or in books because it's impossible[1]!

This function assign a listner pointer to an image widget:
------------------------------------------------------
void image::setListener(AbstractListener *listener)
{
this->listener = listener;
}
------------------------------------------------------
this function use the listner:

------------------------------------------------------
bool image::isMouseOver(GLint mouseX, GLint mouseY)
{
bool isOver = false;
if (mouseX > positionX
&& mouseX < positionX + width
&& mouseY > positionY
&& mouseY < positionY + height)
{
listener->mouseOver(mouseX, mouseY, userValue);
isOver = true;
}
return isOver;
};
------------------------------------------------------

Of course, If I call the function above, without call setListner before,
the application crash.

So I must check listner is OK.
I'm trying somewhat like:

------------------------------------------------------
bool image::isMouseOver(GLint mouseX, GLint mouseY)
{
bool isOver = false;
if (mouseX > positionX
&& mouseX < positionX + width
&& mouseY > positionY
&& mouseY < positionY + height)
{
if (listener)
{
listener->mouseOver(mouseX, mouseY, userValue);
}
isOver = true;
}
return isOver;
};
------------------------------------------------------

But it crash anyway...


The only suggestion I would have would be to initialize the listener to
NULL (0) -- otherwise there's no way to distinguish between
uninitialized garbage and a valid value.

HTH,
--ag

[1] There may be a way to perform a `sanity check' in a platform
specific way -- but even that would not be reliable (and nothing *I*
would use for production code).
--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com
http://www.cafepress.com/goldsays
"If you have nothing to hide, you're not trying!"
Jan 9 '06 #4
> in the constructor of image object, set listener pointer to zero.
It works, THANKS GUYS!
Jan 9 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ron Holmes | last post by:
I posted this question on the Crystal Reports Support site and I am still waiting for an answer. Using Crystal Reports 9.0 Developer Full edition: My Crystal report .RPT file has a Picture box...
13
by: Jack MacRank | last post by:
Hello, I'm coding a webform application in C# (ASP.NET 1.1 SP1 with VS.NET 2003 Pro on WinXP SP2 using IIS 5.1). I created a seperate "data" class to house all the MySQL connection and sql...
11
by: Roy Lawson | last post by:
I have no idea what is going on here. I wrote a simple application in VB.NET to generate a Crystal Report, and I am now trying to move it to ASP.NET with Crstal Enterprise. I wish I could tell...
8
by: Charles | last post by:
I do not understand why I am getting a "Specified cast is not valid" error, since it has worked before. Something has changed and I am not really sure what it could be. I am looking for something...
3
by: Pieter Coucke | last post by:
Hi, In my VB.NET 2005 application I'm generating and sending emails using the outlook-object model (2003). When a mail is Send (MailObject_Send), I raise an event in a global class, that is...
3
by: Jayyde | last post by:
Not sure why all of a sudden this line of code: cmbProductCategoryTreeDisplayName.SelectedIndex = 0; is producing a "Specified cast is not valid error". In the watch it has 1 item in it at...
1
by: illegal.prime | last post by:
Hey all, I have an app, that could take two numbers of any type of numerical type int, long, double, float, uint, ulong, etc. I want to check that the numbers are part of a range that I consider...
12
by: Pao | last post by:
Hi all For all NEW sites (virtual directories) that I create, I receive always the same error: (I translate so may be a little different) Impossible to visualize the XML page Impossible to...
7
by: =?Utf-8?B?Sm9hY2hpbQ==?= | last post by:
I have an image which I'm trying to save using my_image.Save(some_path, System.Drawing.Imaging.ImageFormat.Path); and then I get the error "Parameter is not valid". What could be the reason...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.