Thursday, 29 March 2018

"The Club" where Jerry Seinfeld performed

At the beginning and end of every Seinfeld episode, Jerry performs a bit of stand-up themed alongside the general plot of that particular episode.


Is the club where he performs this stand-up ever identified? If so, what was its name?


Answer


This page provides info on Seinfeld filming locations and it seems that the club's name is Catch A Rising Star Comedy Club located in New York.



Jerry Seinfeld used to perform stand up at the Catch A Rising Star
Comedy Club
, which is now the Vudu Lounge at 1487 1st Avenue. In the
late 90's the club was located at 253 W. 28th Street, which was seen
in the show.



Here is the link to the website of the club.


sqlite - Android bulk insert syntax error near ","

I would appreciate a shade of enlightenment on the following code, since I just can't seem to figure out why it has a syntax error near ",".



I'm trying to insert multiple rows at once in a single statement. I'm targeting API 10+. If I only have one set of VALUES(...) it's all good, and the row gets inserted, but as soon as I have more than one, it sends out an exception with the syntax error message.



SQLiteDatabase db = getWritableDatabase();
String insertRows = "INSERT INTO " + TABLE_FRIENDS +

" (" + KEY_USER_EMAIL + ", " + KEY_FRIEND + ", " + KEY_FRIEND_INTERACTIONS +
", " + KEY_ENABLED +") VALUES ";

int index;
for(index=0; index insertRows += "('" + userAccount.getEmail() +"', ";

Friend friend = friends.get(index);

insertRows+= "'" + friend.getEmail() +"', ";

insertRows+= "'" + friend.getInteractions() +"', ";

int enabled = friend.isEnabled() ? 1 : 0;

insertRows+= "'"+ enabled + "')";

if(index != friends.size()-1)
insertRows+= ",";
}


insertRows += ";";

db.execSQL(insertRows);
db.close();


I print the query out on my log and get:



INSERT INTO friends (user_email, friend_email, friend_interactions, friend_enabled) VALUES ('test2@test.com', 'vila@hotmail.com', '0', '0'),
('test2@test.com', 'anotherone@hotmail.com', '0', '0'),

('test2@test.com', 'yetanother@hotmail.com', '0', '0');


Help is very much appreciated.

sql server - T-SQL: Combine rows to one row




I have the following table:



number  word
====== ====
1 AAA
2 BBB
2 CCCC
4 CCCC

4 BBB
4 AAA


Now I want to create a new table, where a "number" only occurs in one row. The corresponding values in "word" should be converted to comma sepeareted string.



The result table:



number  word
====== ====

1 AAA
2 BBB,CCCC
4 CCCCC,BBB,AAA


How can this solved with T-SQL? Thanks in advance.


Answer



I started so I may as well post mine too...



CREATE TABLE #test

(
ID tinyint
,Word varchar(20)
);
INSERT INTO #test
VALUES
(1,'aaa')
,(1,'bbb')
,(2,'abc')
,(2,'def')

,(2,'ghi')
,(3,'zzz');

SELECT DISTINCT
a.ID
,STUFF((
SELECT
',' + b.Word
FROM #test b
WHERE a.ID = b.ID

FOR XML PATH('')
),1,1,'') AS [Contains]
FROM #test a

c - Difference between malloc and calloc?



What is the difference between doing:




ptr = (char **) malloc (MAXELEMS * sizeof(char *));


or:



ptr = (char **) calloc (MAXELEMS, sizeof(char*));


When is it a good idea to use calloc over malloc or vice versa?


Answer




calloc() zero-initializes the buffer, while malloc() leaves the memory uninitialized.



EDIT:



Zeroing out the memory may take a little time, so you probably want to use malloc() if that performance is an issue. If initializing the memory is more important, use calloc(). For example, calloc() might save you a call to memset().


Wednesday, 28 March 2018

asp.net - Getting configuration settings from web.config/app.config using class library



Configuration settings in 3.5 is driving me nuts... Help! ;)



I have a class library (Named ADI), that needs some configuration settings from the project using it (like connectionstring, filesystem locations etc).



I want to define these settings in my Windows Forms/Web Projects App.Config or Web.Config, like other settings.




Here is part of my app.config for my windows forms application:






C:\DataTemp\ADI\Original\


C:\DataTemp\ADI\Variants\






How do I access that from my class library??



I tried this:



System.Configuration.ConfigurationManager.AppSettings("ADIImageVariantsRoot")



What to do?


Answer



If you're not after structured settings, the appSettings section just takes key-value pairs:










This will enable you to access them via the AppSettings dictionary:



ConfigurationManager.AppSettings["ADIImageVariantsRoot"]


As you would expect.



Alternatively, if you need more structure to your configuration (i.e. more than just strings, or a collection of settings), you can look into using a configuration section of your own, using a ConfigurationSection, and its relevant parts.


c# - Proper use of the IDisposable interface



I know from reading the Microsoft documentation that the "primary" use of the IDisposable interface is to clean up unmanaged resources.



To me, "unmanaged" means things like database connections, sockets, window handles, etc. But, I've seen code where the Dispose() method is implemented to free managed resources, which seems redundant to me, since the garbage collector should take care of that for you.




For example:



public class MyCollection : IDisposable
{
private List _theList = new List();
private Dictionary _theDict = new Dictionary();

// Die, clear it up! (free unmanaged resources)
public void Dispose()

{
_theList.clear();
_theDict.clear();
_theList = null;
_theDict = null;
}


My question is, does this make the garbage collector free memory used by MyCollection any faster than it normally would?




edit: So far people have posted some good examples of using IDisposable to clean up unmanaged resources such as database connections and bitmaps. But suppose that _theList in the above code contained a million strings, and you wanted to free that memory now, rather than waiting for the garbage collector. Would the above code accomplish that?


Answer



The point of Dispose is to free unmanaged resources. It needs to be done at some point, otherwise they will never be cleaned up. The garbage collector doesn't know how to call DeleteHandle() on a variable of type IntPtr, it doesn't know whether or not it needs to call DeleteHandle().




Note: What is an unmanaged resource? If you found it in the Microsoft .NET Framework: it's managed. If you went poking around MSDN yourself, it's unmanaged. Anything you've used P/Invoke calls to get outside of the nice comfy world of everything available to you in the .NET Framework is unmanaged – and you're now responsible for cleaning it up.




The object that you've created needs to expose some method, that the outside world can call, in order to clean up unmanaged resources. The method can be named whatever you like:




public void Cleanup()


or



public void Shutdown()


But instead there is a standardized name for this method:




public void Dispose()


There was even an interface created, IDisposable, that has just that one method:



public interface IDisposable
{
void Dispose()
}



So you make your object expose the IDisposable interface, and that way you promise that you've written that single method to clean up your unmanaged resources:



public void Dispose()
{
Win32.DestroyHandle(this.CursorFileBitmapIconServiceHandle);
}


And you're done. Except you can do better.







What if your object has allocated a 250MB System.Drawing.Bitmap (i.e. the .NET managed Bitmap class) as some sort of frame buffer? Sure, this is a managed .NET object, and the garbage collector will free it. But do you really want to leave 250MB of memory just sitting there – waiting for the garbage collector to eventually come along and free it? What if there's an open database connection? Surely we don't want that connection sitting open, waiting for the GC to finalize the object.



If the user has called Dispose() (meaning they no longer plan to use the object) why not get rid of those wasteful bitmaps and database connections?



So now we will:





  • get rid of unmanaged resources (because we have to), and

  • get rid of managed resources (because we want to be helpful)



So let's update our Dispose() method to get rid of those managed objects:



public void Dispose()
{
//Free unmanaged resources
Win32.DestroyHandle(this.CursorFileBitmapIconServiceHandle);


//Free managed resources too
if (this.databaseConnection != null)
{
this.databaseConnection.Dispose();
this.databaseConnection = null;
}
if (this.frameBufferImage != null)
{
this.frameBufferImage.Dispose();

this.frameBufferImage = null;
}
}


And all is good, except you can do better!






What if the person forgot to call Dispose() on your object? Then they would leak some unmanaged resources!





Note: They won't leak managed resources, because eventually the garbage collector is going to run, on a background thread, and free the memory associated with any unused objects. This will include your object, and any managed objects you use (e.g. the Bitmap and the DbConnection).




If the person forgot to call Dispose(), we can still save their bacon! We still have a way to call it for them: when the garbage collector finally gets around to freeing (i.e. finalizing) our object.




Note: The garbage collector will eventually free all managed objects.
When it does, it calls the Finalize

method on the object. The GC doesn't know, or
care, about your Dispose method.
That was just a name we chose for
a method we call when we want to get
rid of unmanaged stuff.




The destruction of our object by the Garbage collector is the perfect time to free those pesky unmanaged resources. We do this by overriding the Finalize() method.





Note: In C#, you don't explicitly override the Finalize() method.
You write a method that looks like a C++ destructor, and the
compiler takes that to be your implementation of the Finalize() method:




~MyObject()
{
//we're being finalized (i.e. destroyed), call Dispose in case the user forgot to
Dispose(); //<--Warning: subtle bug! Keep reading!
}



But there's a bug in that code. You see, the garbage collector runs on a background thread; you don't know the order in which two objects are destroyed. It is entirely possible that in your Dispose() code, the managed object you're trying to get rid of (because you wanted to be helpful) is no longer there:



public void Dispose()
{
//Free unmanaged resources
Win32.DestroyHandle(this.gdiCursorBitmapStreamFileHandle);

//Free managed resources too

if (this.databaseConnection != null)
{
this.databaseConnection.Dispose(); //<-- crash, GC already destroyed it
this.databaseConnection = null;
}
if (this.frameBufferImage != null)
{
this.frameBufferImage.Dispose(); //<-- crash, GC already destroyed it
this.frameBufferImage = null;
}

}


So what you need is a way for Finalize() to tell Dispose() that it should not touch any managed resources (because they might not be there anymore), while still freeing unmanaged resources.



The standard pattern to do this is to have Finalize() and Dispose() both call a third(!) method; where you pass a Boolean saying if you're calling it from Dispose() (as opposed to Finalize()), meaning it's safe to free managed resources.



This internal method could be given some arbitrary name like "CoreDispose", or "MyInternalDispose", but is tradition to call it Dispose(Boolean):



protected void Dispose(Boolean disposing)



But a more helpful parameter name might be:



protected void Dispose(Boolean itIsSafeToAlsoFreeManagedObjects)
{
//Free unmanaged resources
Win32.DestroyHandle(this.CursorFileBitmapIconServiceHandle);

//Free managed resources too, but only if I'm being called from Dispose

//(If I'm being called from Finalize then the objects might not exist
//anymore
if (itIsSafeToAlsoFreeManagedObjects)
{
if (this.databaseConnection != null)
{
this.databaseConnection.Dispose();
this.databaseConnection = null;
}
if (this.frameBufferImage != null)

{
this.frameBufferImage.Dispose();
this.frameBufferImage = null;
}
}
}


And you change your implementation of the IDisposable.Dispose() method to:




public void Dispose()
{
Dispose(true); //I am calling you from Dispose, it's safe
}


and your finalizer to:



~MyObject()
{

Dispose(false); //I am *not* calling you from Dispose, it's *not* safe
}



Note: If your object descends from an object that implements Dispose, then don't forget to call their base Dispose method when you override Dispose:




public override void Dispose()
{

try
{
Dispose(true); //true: safe to free managed resources
}
finally
{
base.Dispose();
}
}



And all is good, except you can do better!






If the user calls Dispose() on your object, then everything has been cleaned up. Later on, when the garbage collector comes along and calls Finalize, it will then call Dispose again.



Not only is this wasteful, but if your object has junk references to objects you already disposed of from the last call to Dispose(), you'll try to dispose them again!



You'll notice in my code I was careful to remove references to objects that I've disposed, so I don't try to call Dispose on a junk object reference. But that didn't stop a subtle bug from creeping in.




When the user calls Dispose(): the handle CursorFileBitmapIconServiceHandle is destroyed. Later when the garbage collector runs, it will try to destroy the same handle again.



protected void Dispose(Boolean iAmBeingCalledFromDisposeAndNotFinalize)
{
//Free unmanaged resources
Win32.DestroyHandle(this.CursorFileBitmapIconServiceHandle); //<--double destroy
...
}



The way you fix this is tell the garbage collector that it doesn't need to bother finalizing the object – its resources have already been cleaned up, and no more work is needed. You do this by calling GC.SuppressFinalize() in the Dispose() method:



public void Dispose()
{
Dispose(true); //I am calling you from Dispose, it's safe
GC.SuppressFinalize(this); //Hey, GC: don't bother calling finalize later
}



Now that the user has called Dispose(), we have:




  • freed unmanaged resources

  • freed managed resources



There's no point in the GC running the finalizer – everything's taken care of.



Couldn't I use Finalize to cleanup unmanaged resources?




The documentation for Object.Finalize says:




The Finalize method is used to perform cleanup operations on unmanaged resources held by the current object before the object is destroyed.




But the MSDN documentation also says, for IDisposable.Dispose:





Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.




So which is it? Which one is the place for me to cleanup unmanaged resources? The answer is:




It's your choice! But choose Dispose.




You certainly could place your unmanaged cleanup in the finalizer:




~MyObject()
{
//Free unmanaged resources
Win32.DestroyHandle(this.CursorFileBitmapIconServiceHandle);

//A C# destructor automatically calls the destructor of its base class.
}



The problem with that is you have no idea when the garbage collector will get around to finalizing your object. Your un-managed, un-needed, un-used native resources will stick around until the garbage collector eventually runs. Then it will call your finalizer method; cleaning up unmanaged resources. The documentation of Object.Finalize points this out:




The exact time when the finalizer executes is undefined. To ensure deterministic release of resources for instances of your class, implement a Close method or provide a IDisposable.Dispose implementation.




This is the virtue of using Dispose to cleanup unmanaged resources; you get to know, and control, when unmanaged resource are cleaned up. Their destruction is "deterministic".







To answer your original question: Why not release memory now, rather than for when the GC decides to do it? I have a facial recognition software that needs to get rid of 530 MB of internal images now, since they're no longer needed. When we don't: the machine grinds to a swapping halt.



Bonus Reading



For anyone who likes the style of this answer (explaining the why, so the how becomes obvious), I suggest you read Chapter One of Don Box's Essential COM:





In 35 pages he explains the problems of using binary objects, and invents COM before your eyes. Once you realize the why of COM, the remaining 300 pages are obvious, and just detail Microsoft's implementation.




I think every programmer who has ever dealt with objects or COM should, at the very least, read the first chapter. It is the best explanation of anything ever.



Extra Bonus Reading



When everything you know is wrong by Eric Lippert




It is therefore very difficult indeed to write a correct finalizer,
and the best advice I can give you is to not try.




javascript - select option on change



i have json data like this:



[{
"user_id": "113",
"employe_first_name": "Asaladauangkitamakan",
"employe_last_name": "Nasibb"
}, {
"user_id": "105",
"employe_first_name": "Ryan",
"employe_last_name": "Friday"
}, {
"user_id": "87",
"employe_first_name ":"hendi ",
"employe_last_name ":"kenther"
}
]


how to 'create' select option with javascript (option value = user_id, and text = employe_first_name) that load when document ready, and fill another field with selected value


Answer



var data = [ {"user_id":"113","employe_first_name":"Asaladauangkitamakan","employe_last_name":"Nasibb"}, {"user_id":"105","employe_first_name":"Ryan","employe_last_name":"Friday"},   {"user_id":"87","employe_first_name":"hendi","employe_last_name":"kenther"} ];
var select = document.createElement("select");
for(var i = 0; i < data.length; i++){
var option = document.createElement("option");
option.value = data[i].user_id;
option.innerHTML = data[i].employe_first_name;
select.appendChild(option);
}
select.addEventListenter("change", function(event) {
var selected_value = event.target.value;
document.getElementById("another_field").value = selected_value;
});
someDiv.appendChild(select); // Add the select menu to the DOM

casting - Why wasn&#39;t Tobey Maguire in The Amazing Spider-Man? - Movies &amp; TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...