473,396 Members | 2,011 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,396 software developers and data experts.

Find error

is there any error in the following code?

class PrettyMenu
{
public:
void changebackground(std::istream& new);
private:
mutex fmutex;
image *fimage;
int changenum;//record the change times
}

void changebackgroud(std::istream& new)
{
lock(&fmutex);
delete fimage;
++changnum;
fimage=new image(new);
unlock(&fmutex);
}
Nov 30 '07 #1
7 1300
ya*********@gmail.com wrote:
is there any error in the following code?
Several.
class PrettyMenu
{
public:
void changebackground(std::istream& new);
private:
mutex fmutex;
image *fimage;
int changenum;//record the change times
}

void changebackgroud(std::istream& new)
{
lock(&fmutex);
delete fimage;
++changnum;
fimage=new image(new);
unlock(&fmutex);
}
--
Ian Collins.
Nov 30 '07 #2
It is just a paper test,not real code.and where?
Several.
Nov 30 '07 #3
ya*********@gmail.com wrote:

Please don't top post.
>
>Several.
It is just a paper test,not real code.and where?
Homework?

What does your compiler tell you?

Hint: spot the inappropriate use of a keyword.

--
Ian Collins.
Nov 30 '07 #4
ya*********@gmail.com wrote:
is there any error in the following code?

class PrettyMenu
{
public:
void changebackground(std::istream& new);
private:
mutex fmutex;
image *fimage;
int changenum;//record the change times
}

void changebackgroud(std::istream& new)
{
lock(&fmutex);
delete fimage;
++changnum;
fimage=new image(new);
unlock(&fmutex);
}
I am not going to open the standard for this one: even if the code should
turn out formally correct, it should be rewritten anyway.

a) The use of "new" as the name for a parameter should be avoided,
especially if it leads to lines like

fimage = new image (new);

b) The member fimage is a pointer for no reason. Apparently, you allocate
and copy-construct the image anyway (at least in the method shown, the
objects behaves as though it is the exclusive owner of the image), so the
pointer buys you nothing but trouble.

c) The comment for the int is misleading: it makes you think that the int
stores a time stamp.
What about:

class PrettyMenu
{
public:
void changebackground(std::istream& istr);
private:
mutex fmutex;
image fimage;
int changenum;// count the changes
}

void changebackgroud(std::istream& istr)
{
lock(&fmutex);
istr >fimage;
++changnum;
unlock(&fmutex);
}
Best

Kai-Uwe Bux
Nov 30 '07 #5
On Thu, 29 Nov 2007 23:42:52 -0800 (PST), ya*********@gmail.com wrote:
is there any error in the following code?
Apart from the obvious problems preventing successful
compilation, I'll point one less obvious one:
void changebackgroud(std::istream& new)
{
lock(&fmutex);
delete fimage;
++changnum;
fimage=new image(new);
unlock(&fmutex);
}
If 'new' throws an exception here, fmutex remains
locked (unlock is not on the return part) and nobody
can lock it again.

Instead of littering all code with try...catch constructs,
I usually solve these by putting all the cleanup operations
in an ad-hoc scoped destructor.
For example:

void foo()
{
struct autolockmutex
{
mutex& m;
autolockmutex(mutex& mm) : m(mm) { lock(&m); }
~autolockmutex() { unlock(&m); }
} autounlock(fmutex);

delete...
...other stuff...
...a call to new...
...possibly some other stuff...
...but no mutex handling stuff needed anymore...
}
This way, no matter which way the function is exited*,
autolockmutex() will unlock the mutex at the end
of the function's scope.
Of course, this is so a common usecase that it is better
to create a global scoped lock class for the purpose.

To limit the scope in which the mutex is locked, you
can instantiate the autoclass inside a {} scope; it
will unlock it at the end of that scope, regardless
of the exit path.

*) except abort(), crash, etc...

--
Joel Yliluoma - http://iki.fi/bisqwit/
Nov 30 '07 #6
On Thu, 29 Nov 2007 23:42:52 -0800 (PST), ya*********@gmail.com wrote:
is there any error in the following code?
Apart from the obvious problems preventing successful
compilation, I'll point one less obvious one:
void changebackgroud(std::istream& new)
{
lock(&fmutex);
delete fimage;
++changnum;
fimage=new image(new);
unlock(&fmutex);
}
If 'new' throws an exception here, fmutex remains
locked (unlock is not on the return path) and nobody
can lock it again.

Instead of littering all code with try...catch constructs,
I usually solve these by putting all the cleanup operations
in an ad-hoc scoped destructor.
For example:

void foo()
{
struct autolockmutex
{
mutex& m;
autolockmutex(mutex& mm) : m(mm) { lock(&m); }
~autolockmutex() { unlock(&m); }
} autounlock(fmutex);

...all the normal function code here...
...except the mutex handling. ...
}
This way, no matter which way the function is exited*,
autolockmutex() will unlock the mutex at the end
of the function's scope.
Of course, this is so a common usecase that it is better
to create a global scoped lock class for the purpose.

To limit the scope in which the mutex is locked, you
can instantiate the autoclass inside a {} scope; it
will unlock it at the end of that scope, regardless
of the exit path.

*) except abort(), crash, etc...

--
Joel Yliluoma - http://iki.fi/bisqwit/
Nov 30 '07 #7
On Nov 30, 11:09 am, Joel Yliluoma <bisq...@iki.fiwrote:
On Thu, 29 Nov 2007 23:42:52 -0800 (PST), yayalee1...@gmail.com wrote:
is there any error in the following code?
Apart from the obvious problems preventing successful
compilation, I'll point one less obvious one:
void changebackgroud(std::istream& new)
{
lock(&fmutex);
delete fimage;
++changnum;
fimage=new image(new);
unlock(&fmutex);
}
If 'new' throws an exception here, fmutex remains
locked (unlock is not on the return path) and nobody
can lock it again.
If 'new' throws an exception here, he can no longer access the
object, not even to destruct it. Loosing the lock is part of
the problem, but leaving an invalid pointer in fimage is also a
fatal error.

In short, he must do everything that can fail before the delete
(which leaves the pointer invalid). And the lock isn't
necessary except around the parts which can't fail, either.
(It's probably not desirable for the creation of the new image
to be protected by the lock either, since it's likely to be a
very long process.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 30 '07 #8

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

Similar topics

1
by: Xah Lee | last post by:
suppose you want to do find & replace of string of all files in a directory. here's the code: ©# -*- coding: utf-8 -*- ©# Python © ©import os,sys © ©mydir= '/Users/t/web'
108
by: Bryan Olson | last post by:
The Python slice type has one method 'indices', and reportedly: This method takes a single integer argument /length/ and computes information about the extended slice that the slice object would...
0
by: Andrei Ivanov | last post by:
Hello, it seems my postgresql data has somehow become corrupted (by a forced shutdown I think): psql template1 -U shadow Password: ERROR: nodeRead: did not find '}' at end of plan node...
1
by: Andrei Ivanov | last post by:
Hello, it seems my postgresql data has somehow become corrupted (by a forced shutdown I think): psql template1 -U shadow Password: ERROR: nodeRead: did not find '}' at end of plan node...
2
by: Zenobia | last post by:
Hi, Below is a bit of code from ASP.NET Unleashed which gives an error and I can't figure out why. It uses the Authors table from the standard Pubs database. The error message is...
7
by: tehn.yit.chin | last post by:
I am trying to experiment <algorithm>'s find to search for an item in a vector of struct. My bit of test code is shown below. #include <iostream> #include <vector> #include <algorithm>...
0
by: Curious | last post by:
Hi, I'm not sure if this is the right place to post such command issues. If you know a better forum where people respond to messages fairly often, please let me know! Anyway, would appreciate...
3
by: Lance Wynn | last post by:
Hello, I am receiving this error when trying to instantiate a webservice component. I have 2 development machines, both are XP sp2 with VS 2008 installed. On one machine, the code works fine. On...
2
by: Olumide | last post by:
Hello, I've got this nice inner class that I'm holds a set of "FrontPoint" objects as shown below. Unfortunately, the find and insert methods trigger massive C2784 errors. Would someone please...
11
by: ezechiel | last post by:
hi, I have the following error on a win server: "Invalid Top Directory perl/lib/file/find.pm line 598 Here's the code and I'll explain what I found until now: #!/usr/bin/perl # use strict;...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.